[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 | // |
---|
| 7 | // XXX.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : 0 |
---|
| 14 | // |
---|
[51b7345] | 15 | /* |
---|
| 16 | * This file is part of the Cforall project |
---|
| 17 | * |
---|
| 18 | * $Id: PolyMutator.cc,v 1.7 2005/08/29 20:14:13 rcbilson Exp $ |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "PolyMutator.h" |
---|
| 23 | #include "SynTree/Declaration.h" |
---|
| 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/Expression.h" |
---|
| 26 | #include "SynTree/Statement.h" |
---|
| 27 | #include "SynTree/Mutator.h" |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | namespace GenPoly { |
---|
| 31 | |
---|
| 32 | namespace { |
---|
| 33 | const std::list<Label> noLabels; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | PolyMutator::PolyMutator() |
---|
| 37 | : env( 0 ) |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void |
---|
| 42 | PolyMutator::mutateStatementList( std::list< Statement* > &statements ) |
---|
| 43 | { |
---|
[a32b204] | 44 | for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { |
---|
| 45 | if ( ! stmtsToAddAfter.empty() ) { |
---|
[51b7345] | 46 | statements.splice( i, stmtsToAddAfter ); |
---|
| 47 | } |
---|
| 48 | *i = (*i)->acceptMutator( *this ); |
---|
[a32b204] | 49 | if ( ! stmtsToAdd.empty() ) { |
---|
[51b7345] | 50 | statements.splice( i, stmtsToAdd ); |
---|
| 51 | } |
---|
| 52 | } |
---|
[a32b204] | 53 | if ( ! stmtsToAddAfter.empty() ) { |
---|
[51b7345] | 54 | statements.splice( statements.end(), stmtsToAddAfter ); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | Statement* |
---|
| 59 | PolyMutator::mutateStatement( Statement *stmt ) |
---|
| 60 | { |
---|
| 61 | Statement *newStmt = maybeMutate( stmt, *this ); |
---|
[a32b204] | 62 | if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { |
---|
[51b7345] | 63 | CompoundStmt *compound = new CompoundStmt( noLabels ); |
---|
| 64 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); |
---|
| 65 | compound->get_kids().push_back( newStmt ); |
---|
| 66 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); |
---|
[b1a6d6b] | 67 | // doEndScope(); |
---|
[51b7345] | 68 | return compound; |
---|
| 69 | } else { |
---|
| 70 | return newStmt; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | Expression* |
---|
| 75 | PolyMutator::mutateExpression( Expression *expr ) |
---|
| 76 | { |
---|
[a32b204] | 77 | if ( expr ) { |
---|
| 78 | if ( expr->get_env() ) { |
---|
[51b7345] | 79 | env = expr->get_env(); |
---|
| 80 | } |
---|
| 81 | return expr->acceptMutator( *this ); |
---|
| 82 | } else { |
---|
| 83 | return expr; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | CompoundStmt* |
---|
| 88 | PolyMutator::mutate(CompoundStmt *compoundStmt) |
---|
| 89 | { |
---|
[b1a6d6b] | 90 | doBeginScope(); |
---|
[51b7345] | 91 | mutateStatementList( compoundStmt->get_kids() ); |
---|
| 92 | doEndScope(); |
---|
| 93 | return compoundStmt; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | Statement* |
---|
| 97 | PolyMutator::mutate(IfStmt *ifStmt) |
---|
| 98 | { |
---|
| 99 | ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) ); |
---|
| 100 | ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) ); |
---|
| 101 | ifStmt->set_condition( mutateExpression( ifStmt->get_condition() ) ); |
---|
| 102 | return ifStmt; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | Statement* |
---|
| 106 | PolyMutator::mutate(WhileStmt *whileStmt) |
---|
| 107 | { |
---|
| 108 | whileStmt->set_body( mutateStatement( whileStmt->get_body() ) ); |
---|
| 109 | whileStmt->set_condition( mutateExpression( whileStmt->get_condition() ) ); |
---|
| 110 | return whileStmt; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | Statement* |
---|
| 114 | PolyMutator::mutate(ForStmt *forStmt) |
---|
| 115 | { |
---|
| 116 | forStmt->set_body( mutateStatement( forStmt->get_body() ) ); |
---|
| 117 | forStmt->set_initialization( maybeMutate( forStmt->get_initialization(), *this ) ); |
---|
| 118 | forStmt->set_condition( mutateExpression( forStmt->get_condition() ) ); |
---|
| 119 | forStmt->set_increment( mutateExpression( forStmt->get_increment() ) ); |
---|
| 120 | return forStmt; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | Statement* |
---|
| 124 | PolyMutator::mutate(SwitchStmt *switchStmt) |
---|
| 125 | { |
---|
| 126 | mutateStatementList( switchStmt->get_branches() ); |
---|
| 127 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); |
---|
| 128 | return switchStmt; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | Statement* |
---|
| 132 | PolyMutator::mutate(ChooseStmt *switchStmt) |
---|
| 133 | { |
---|
| 134 | mutateStatementList( switchStmt->get_branches() ); |
---|
| 135 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); |
---|
| 136 | return switchStmt; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | Statement* |
---|
| 140 | PolyMutator::mutate(CaseStmt *caseStmt) |
---|
| 141 | { |
---|
| 142 | mutateStatementList( caseStmt->get_statements() ); |
---|
| 143 | caseStmt->set_condition( mutateExpression( caseStmt->get_condition() ) ); |
---|
| 144 | |
---|
| 145 | return caseStmt; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | Statement* |
---|
| 149 | PolyMutator::mutate(TryStmt *tryStmt) |
---|
| 150 | { |
---|
| 151 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
| 152 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
| 153 | |
---|
| 154 | return tryStmt; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | Statement* |
---|
| 158 | PolyMutator::mutate(CatchStmt *cathStmt) |
---|
| 159 | { |
---|
| 160 | cathStmt->set_body( mutateStatement( cathStmt->get_body() ) ); |
---|
| 161 | cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) ); |
---|
| 162 | return cathStmt; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | Statement* |
---|
| 166 | PolyMutator::mutate(ReturnStmt *retStmt) |
---|
| 167 | { |
---|
| 168 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); |
---|
| 169 | return retStmt; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | Statement* |
---|
| 173 | PolyMutator::mutate(ExprStmt *exprStmt) |
---|
| 174 | { |
---|
| 175 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); |
---|
| 176 | return exprStmt; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | Expression* |
---|
| 181 | PolyMutator::mutate(UntypedExpr *untypedExpr) |
---|
| 182 | { |
---|
[a32b204] | 183 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { |
---|
[51b7345] | 184 | *i = mutateExpression( *i ); |
---|
| 185 | } |
---|
| 186 | return untypedExpr; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /* static class method */ |
---|
| 190 | void |
---|
| 191 | PolyMutator::makeTyVarMap( Type *type, TyVarMap &tyVarMap ) |
---|
| 192 | { |
---|
[a32b204] | 193 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { |
---|
[51b7345] | 194 | assert( *tyVar ); |
---|
| 195 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); |
---|
| 196 | } |
---|
[a32b204] | 197 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { |
---|
[51b7345] | 198 | makeTyVarMap( pointer->get_base(), tyVarMap ); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /* static class method */ |
---|
| 203 | } // namespace GenPoly |
---|
[51587aa] | 204 | // Local Variables: // |
---|
| 205 | // tab-width: 4 // |
---|
| 206 | // mode: c++ // |
---|
| 207 | // compile-command: "make install" // |
---|
| 208 | // End: // |
---|