| 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 : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Thu Jun 22 13:47:00 2017
 | 
|---|
| 13 | // Update Count     : 17
 | 
|---|
| 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 |         PolyMutator::PolyMutator() : scopeTyVars( TypeDecl::Data{} ), env( 0 ) {}
 | 
|---|
| 26 | 
 | 
|---|
| 27 |         void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) {
 | 
|---|
| 28 |                 SemanticError errors;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |                 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
 | 
|---|
| 31 |                         if ( ! stmtsToAddAfter.empty() ) {
 | 
|---|
| 32 |                                 statements.splice( i, stmtsToAddAfter );
 | 
|---|
| 33 |                         } // if
 | 
|---|
| 34 |                         try {
 | 
|---|
| 35 |                                 *i = (*i)->acceptMutator( *this );
 | 
|---|
| 36 |                         } catch ( SemanticError &e ) {
 | 
|---|
| 37 |                                 errors.append( e );
 | 
|---|
| 38 |                         } // try
 | 
|---|
| 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
 | 
|---|
| 46 |                 if ( ! errors.isEmpty() ) {
 | 
|---|
| 47 |                         throw errors;
 | 
|---|
| 48 |                 }
 | 
|---|
| 49 |         }
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         Statement * PolyMutator::mutateStatement( Statement *stmt ) {
 | 
|---|
| 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 | 
 | 
|---|
| 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 |                         }
 | 
|---|
| 77 |                         // xxx - should env be cloned (or moved) onto the result of the mutate?
 | 
|---|
| 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) {
 | 
|---|
| 92 |                 ifStmt->set_condition(  mutateExpression( ifStmt->get_condition() ) );
 | 
|---|
| 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() ) );
 | 
|---|
| 100 |                 whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) );
 | 
|---|
| 101 |                 return whileStmt;
 | 
|---|
| 102 |         }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         Statement * PolyMutator::mutate(ForStmt *forStmt) {
 | 
|---|
| 105 |                 mutateAll( forStmt->get_initialization(), *this );
 | 
|---|
| 106 |                 forStmt->set_condition(  mutateExpression( forStmt->get_condition() ) );
 | 
|---|
| 107 |                 forStmt->set_increment(  mutateExpression( forStmt->get_increment() ) );
 | 
|---|
| 108 |                 forStmt->set_body(  mutateStatement( forStmt->get_body() ) );
 | 
|---|
| 109 |                 return forStmt;
 | 
|---|
| 110 |         }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         Statement * PolyMutator::mutate(SwitchStmt *switchStmt) {
 | 
|---|
| 113 |                 switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) );
 | 
|---|
| 114 |                 mutateStatementList( switchStmt->get_statements() );
 | 
|---|
| 115 |                 return switchStmt;
 | 
|---|
| 116 |         }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         Statement * PolyMutator::mutate(CaseStmt *caseStmt) {
 | 
|---|
| 119 |                 caseStmt->set_condition(  mutateExpression( caseStmt->get_condition() ) );
 | 
|---|
| 120 |                 mutateStatementList( caseStmt->get_statements() );
 | 
|---|
| 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 |                 tryStmt->set_finally( maybeMutate( tryStmt->get_finally(), *this ) );
 | 
|---|
| 128 |                 return tryStmt;
 | 
|---|
| 129 |         }
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         Statement * PolyMutator::mutate(CatchStmt *cathStmt) {
 | 
|---|
| 132 |                 cathStmt->set_body( mutateStatement( cathStmt->get_body() ) );
 | 
|---|
| 133 |                 cathStmt->set_cond( maybeMutate( cathStmt->get_cond(), *this ) );
 | 
|---|
| 134 |                 cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) );
 | 
|---|
| 135 |                 return cathStmt;
 | 
|---|
| 136 |         }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         Statement * PolyMutator::mutate(ReturnStmt *retStmt) {
 | 
|---|
| 139 |                 retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
 | 
|---|
| 140 |                 return retStmt;
 | 
|---|
| 141 |         }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |         Statement * PolyMutator::mutate(ExprStmt *exprStmt) {
 | 
|---|
| 144 |                 exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) );
 | 
|---|
| 145 |                 return exprStmt;
 | 
|---|
| 146 |         }
 | 
|---|
| 147 | 
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) {
 | 
|---|
| 150 |                 for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) {
 | 
|---|
| 151 |                         *i = mutateExpression( *i );
 | 
|---|
| 152 |                 } // for
 | 
|---|
| 153 |                 return untypedExpr;
 | 
|---|
| 154 |         }
 | 
|---|
| 155 | 
 | 
|---|
| 156 |         Expression *PolyMutator::mutate( StmtExpr * stmtExpr ) {
 | 
|---|
| 157 |                 // don't want statements from outer CompoundStmts to be added to this StmtExpr
 | 
|---|
| 158 |                 ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd );
 | 
|---|
| 159 |                 ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter );
 | 
|---|
| 160 |                 ValueGuard< TypeSubstitution * > oldEnv( env );
 | 
|---|
| 161 | 
 | 
|---|
| 162 |                 // xxx - not sure if this is needed, along with appropriate reset, but I don't think so...
 | 
|---|
| 163 |                 // ValueGuard< TyVarMap > oldScopeTyVars( scopeTyVars );
 | 
|---|
| 164 | 
 | 
|---|
| 165 |                 stmtsToAdd.clear();
 | 
|---|
| 166 |                 stmtsToAddAfter.clear();
 | 
|---|
| 167 |                 // scopeTyVars.clear();
 | 
|---|
| 168 | 
 | 
|---|
| 169 |                 return Parent::mutate( stmtExpr );
 | 
|---|
| 170 |         }
 | 
|---|
| 171 | 
 | 
|---|
| 172 |         Initializer *PolyMutator::mutate( SingleInit *singleInit ) {
 | 
|---|
| 173 |                 singleInit->set_value( mutateExpression( singleInit->get_value() ) );
 | 
|---|
| 174 |                 return singleInit;
 | 
|---|
| 175 |         }
 | 
|---|
| 176 | } // namespace GenPoly
 | 
|---|
| 177 | 
 | 
|---|
| 178 | // Local Variables: //
 | 
|---|
| 179 | // tab-width: 4 //
 | 
|---|
| 180 | // mode: c++ //
 | 
|---|
| 181 | // compile-command: "make install" //
 | 
|---|
| 182 | // End: //
 | 
|---|