| 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 | { | 
|---|
| 30 | for( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { | 
|---|
| 31 | if( !stmtsToAddAfter.empty() ) { | 
|---|
| 32 | statements.splice( i, stmtsToAddAfter ); | 
|---|
| 33 | } | 
|---|
| 34 | *i = (*i)->acceptMutator( *this ); | 
|---|
| 35 | if( !stmtsToAdd.empty() ) { | 
|---|
| 36 | statements.splice( i, stmtsToAdd ); | 
|---|
| 37 | } | 
|---|
| 38 | } | 
|---|
| 39 | if( !stmtsToAddAfter.empty() ) { | 
|---|
| 40 | statements.splice( statements.end(), stmtsToAddAfter ); | 
|---|
| 41 | } | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | Statement* | 
|---|
| 45 | PolyMutator::mutateStatement( Statement *stmt ) | 
|---|
| 46 | { | 
|---|
| 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* | 
|---|
| 61 | PolyMutator::mutateExpression( Expression *expr ) | 
|---|
| 62 | { | 
|---|
| 63 | if( expr ) { | 
|---|
| 64 | if( expr->get_env() ) { | 
|---|
| 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 | { | 
|---|
| 76 | mutateStatementList( compoundStmt->get_kids() ); | 
|---|
| 77 | doEndScope(); | 
|---|
| 78 | return compoundStmt; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | Statement* | 
|---|
| 82 | PolyMutator::mutate(IfStmt *ifStmt) | 
|---|
| 83 | { | 
|---|
| 84 | ifStmt->set_thenPart(  mutateStatement( ifStmt->get_thenPart() ) ); | 
|---|
| 85 | ifStmt->set_elsePart(  mutateStatement( ifStmt->get_elsePart() ) ); | 
|---|
| 86 | ifStmt->set_condition(  mutateExpression( ifStmt->get_condition() ) ); | 
|---|
| 87 | return ifStmt; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | Statement* | 
|---|
| 91 | PolyMutator::mutate(WhileStmt *whileStmt) | 
|---|
| 92 | { | 
|---|
| 93 | whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) ); | 
|---|
| 94 | whileStmt->set_condition(  mutateExpression( whileStmt->get_condition() ) ); | 
|---|
| 95 | return whileStmt; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | Statement* | 
|---|
| 99 | PolyMutator::mutate(ForStmt *forStmt) | 
|---|
| 100 | { | 
|---|
| 101 | forStmt->set_body(  mutateStatement( forStmt->get_body() ) ); | 
|---|
| 102 | forStmt->set_initialization(  maybeMutate( forStmt->get_initialization(), *this ) ); | 
|---|
| 103 | forStmt->set_condition(  mutateExpression( forStmt->get_condition() ) ); | 
|---|
| 104 | forStmt->set_increment(  mutateExpression( forStmt->get_increment() ) ); | 
|---|
| 105 | return forStmt; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | Statement* | 
|---|
| 109 | PolyMutator::mutate(SwitchStmt *switchStmt) | 
|---|
| 110 | { | 
|---|
| 111 | mutateStatementList( switchStmt->get_branches() ); | 
|---|
| 112 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); | 
|---|
| 113 | return switchStmt; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | Statement* | 
|---|
| 117 | PolyMutator::mutate(ChooseStmt *switchStmt) | 
|---|
| 118 | { | 
|---|
| 119 | mutateStatementList( switchStmt->get_branches() ); | 
|---|
| 120 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); | 
|---|
| 121 | return switchStmt; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | Statement* | 
|---|
| 125 | PolyMutator::mutate(CaseStmt *caseStmt) | 
|---|
| 126 | { | 
|---|
| 127 | mutateStatementList( caseStmt->get_statements() ); | 
|---|
| 128 | caseStmt->set_condition(  mutateExpression( caseStmt->get_condition() ) ); | 
|---|
| 129 |  | 
|---|
| 130 | return caseStmt; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | Statement* | 
|---|
| 134 | PolyMutator::mutate(TryStmt *tryStmt) | 
|---|
| 135 | { | 
|---|
| 136 | tryStmt->set_block(  maybeMutate( tryStmt->get_block(), *this ) ); | 
|---|
| 137 | mutateAll( tryStmt->get_catchers(), *this ); | 
|---|
| 138 |  | 
|---|
| 139 | return tryStmt; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | Statement* | 
|---|
| 143 | PolyMutator::mutate(CatchStmt *cathStmt) | 
|---|
| 144 | { | 
|---|
| 145 | cathStmt->set_body(  mutateStatement( cathStmt->get_body() ) ); | 
|---|
| 146 | cathStmt->set_decl(  maybeMutate( cathStmt->get_decl(), *this ) ); | 
|---|
| 147 | return cathStmt; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | Statement* | 
|---|
| 151 | PolyMutator::mutate(ReturnStmt *retStmt) | 
|---|
| 152 | { | 
|---|
| 153 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); | 
|---|
| 154 | return retStmt; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | Statement* | 
|---|
| 158 | PolyMutator::mutate(ExprStmt *exprStmt) | 
|---|
| 159 | { | 
|---|
| 160 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); | 
|---|
| 161 | return exprStmt; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 | Expression* | 
|---|
| 166 | PolyMutator::mutate(UntypedExpr *untypedExpr) | 
|---|
| 167 | { | 
|---|
| 168 | for( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { | 
|---|
| 169 | *i = mutateExpression( *i ); | 
|---|
| 170 | } | 
|---|
| 171 | return untypedExpr; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | /* static class method */ | 
|---|
| 175 | void | 
|---|
| 176 | PolyMutator::makeTyVarMap( Type *type, TyVarMap &tyVarMap ) | 
|---|
| 177 | { | 
|---|
| 178 | for( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { | 
|---|
| 179 | assert( *tyVar ); | 
|---|
| 180 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); | 
|---|
| 181 | } | 
|---|
| 182 | if( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { | 
|---|
| 183 | makeTyVarMap( pointer->get_base(), tyVarMap ); | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /* static class method */ | 
|---|
| 188 | } // namespace GenPoly | 
|---|