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