| 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 : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Thu Aug  4 11:26:22 2016
 | 
|---|
| 13 | // Update Count     : 16
 | 
|---|
| 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 |                 SemanticError errors;
 | 
|---|
| 33 | 
 | 
|---|
| 34 |                 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
 | 
|---|
| 35 |                         if ( ! stmtsToAddAfter.empty() ) {
 | 
|---|
| 36 |                                 statements.splice( i, stmtsToAddAfter );
 | 
|---|
| 37 |                         } // if
 | 
|---|
| 38 |                         try {
 | 
|---|
| 39 |                                 *i = (*i)->acceptMutator( *this );
 | 
|---|
| 40 |                         } catch ( SemanticError &e ) {
 | 
|---|
| 41 |                                 errors.append( e );
 | 
|---|
| 42 |                         } // try
 | 
|---|
| 43 |                         if ( ! stmtsToAdd.empty() ) {
 | 
|---|
| 44 |                                 statements.splice( i, stmtsToAdd );
 | 
|---|
| 45 |                         } // if
 | 
|---|
| 46 |                 } // for
 | 
|---|
| 47 |                 if ( ! stmtsToAddAfter.empty() ) {
 | 
|---|
| 48 |                         statements.splice( statements.end(), stmtsToAddAfter );
 | 
|---|
| 49 |                 } // if
 | 
|---|
| 50 |                 if ( ! errors.isEmpty() ) {
 | 
|---|
| 51 |                         throw errors;
 | 
|---|
| 52 |                 }
 | 
|---|
| 53 |         }
 | 
|---|
| 54 | 
 | 
|---|
| 55 |         Statement * PolyMutator::mutateStatement( Statement *stmt ) {
 | 
|---|
| 56 |                 Statement *newStmt = maybeMutate( stmt, *this );
 | 
|---|
| 57 |                 if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) {
 | 
|---|
| 58 |                         CompoundStmt *compound = new CompoundStmt( noLabels );
 | 
|---|
| 59 |                         compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd );
 | 
|---|
| 60 |                         compound->get_kids().push_back( newStmt );
 | 
|---|
| 61 |                         compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter );
 | 
|---|
| 62 |                         // doEndScope();
 | 
|---|
| 63 |                         return compound;
 | 
|---|
| 64 |                 } else {
 | 
|---|
| 65 |                         return newStmt;
 | 
|---|
| 66 |                 }
 | 
|---|
| 67 |         }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         Expression * PolyMutator::mutateExpression( Expression *expr ) {
 | 
|---|
| 70 |                 if ( expr ) {
 | 
|---|
| 71 |                         if ( expr->get_env() ) {
 | 
|---|
| 72 |                                 env = expr->get_env();
 | 
|---|
| 73 |                         }
 | 
|---|
| 74 |                         // xxx - should env be cloned (or moved) onto the result of the mutate?
 | 
|---|
| 75 |                         return expr->acceptMutator( *this );
 | 
|---|
| 76 |                 } else {
 | 
|---|
| 77 |                         return expr;
 | 
|---|
| 78 |                 }
 | 
|---|
| 79 |         }
 | 
|---|
| 80 | 
 | 
|---|
| 81 |         CompoundStmt * PolyMutator::mutate(CompoundStmt *compoundStmt) {
 | 
|---|
| 82 |                 doBeginScope();
 | 
|---|
| 83 |                 mutateStatementList( compoundStmt->get_kids() );
 | 
|---|
| 84 |                 doEndScope();
 | 
|---|
| 85 |                 return compoundStmt;
 | 
|---|
| 86 |         }
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         Statement * PolyMutator::mutate(IfStmt *ifStmt) {
 | 
|---|
| 89 |                 ifStmt->set_thenPart(  mutateStatement( ifStmt->get_thenPart() ) );
 | 
|---|
| 90 |                 ifStmt->set_elsePart(  mutateStatement( ifStmt->get_elsePart() ) );
 | 
|---|
| 91 |                 ifStmt->set_condition(  mutateExpression( ifStmt->get_condition() ) );
 | 
|---|
| 92 |                 return ifStmt;
 | 
|---|
| 93 |         }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |         Statement * PolyMutator::mutate(WhileStmt *whileStmt) {
 | 
|---|
| 96 |                 whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) );
 | 
|---|
| 97 |                 whileStmt->set_condition(  mutateExpression( whileStmt->get_condition() ) );
 | 
|---|
| 98 |                 return whileStmt;
 | 
|---|
| 99 |         }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         Statement * PolyMutator::mutate(ForStmt *forStmt) {
 | 
|---|
| 102 |                 forStmt->set_body(  mutateStatement( forStmt->get_body() ) );
 | 
|---|
| 103 |                 mutateAll( forStmt->get_initialization(), *this );
 | 
|---|
| 104 |                 forStmt->set_condition(  mutateExpression( forStmt->get_condition() ) );
 | 
|---|
| 105 |                 forStmt->set_increment(  mutateExpression( forStmt->get_increment() ) );
 | 
|---|
| 106 |                 return forStmt;
 | 
|---|
| 107 |         }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         Statement * PolyMutator::mutate(SwitchStmt *switchStmt) {
 | 
|---|
| 110 |                 mutateStatementList( switchStmt->get_statements() );
 | 
|---|
| 111 |                 switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) );
 | 
|---|
| 112 |                 return switchStmt;
 | 
|---|
| 113 |         }
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         Statement * PolyMutator::mutate(CaseStmt *caseStmt) {
 | 
|---|
| 116 |                 mutateStatementList( caseStmt->get_statements() );
 | 
|---|
| 117 |                 caseStmt->set_condition(  mutateExpression( caseStmt->get_condition() ) );
 | 
|---|
| 118 |                 return caseStmt;
 | 
|---|
| 119 |         }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         Statement * PolyMutator::mutate(TryStmt *tryStmt) {
 | 
|---|
| 122 |                 tryStmt->set_block(  maybeMutate( tryStmt->get_block(), *this ) );
 | 
|---|
| 123 |                 mutateAll( tryStmt->get_catchers(), *this );
 | 
|---|
| 124 |                 return tryStmt;
 | 
|---|
| 125 |         }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         Statement * PolyMutator::mutate(CatchStmt *cathStmt) {
 | 
|---|
| 128 |                 cathStmt->set_body(  mutateStatement( cathStmt->get_body() ) );
 | 
|---|
| 129 |                 cathStmt->set_decl(  maybeMutate( cathStmt->get_decl(), *this ) );
 | 
|---|
| 130 |                 return cathStmt;
 | 
|---|
| 131 |         }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         Statement * PolyMutator::mutate(ReturnStmt *retStmt) {
 | 
|---|
| 134 |                 retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
 | 
|---|
| 135 |                 return retStmt;
 | 
|---|
| 136 |         }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         Statement * PolyMutator::mutate(ExprStmt *exprStmt) {
 | 
|---|
| 139 |                 exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) );
 | 
|---|
| 140 |                 return exprStmt;
 | 
|---|
| 141 |         }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) {
 | 
|---|
| 145 |                 for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) {
 | 
|---|
| 146 |                         *i = mutateExpression( *i );
 | 
|---|
| 147 |                 } // for
 | 
|---|
| 148 |                 return untypedExpr;
 | 
|---|
| 149 |         }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         Initializer *PolyMutator::mutate( SingleInit *singleInit ) {
 | 
|---|
| 153 |                 singleInit->set_value( mutateExpression( singleInit->get_value() ) );
 | 
|---|
| 154 |                 return singleInit;
 | 
|---|
| 155 |         }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | } // namespace GenPoly
 | 
|---|
| 158 | 
 | 
|---|
| 159 | // Local Variables: //
 | 
|---|
| 160 | // tab-width: 4 //
 | 
|---|
| 161 | // mode: c++ //
 | 
|---|
| 162 | // compile-command: "make install" //
 | 
|---|
| 163 | // End: //
 | 
|---|