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