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 : Andrew Beach |
---|
12 | // Last Modified On : Thu Jun 22 13:47:00 2017 |
---|
13 | // Update Count : 17 |
---|
14 | // |
---|
15 | |
---|
16 | #include "PolyMutator.h" |
---|
17 | |
---|
18 | #include "Common/SemanticError.h" // for SemanticError |
---|
19 | #include "Common/utility.h" // for ValueGuard |
---|
20 | #include "SynTree/Declaration.h" // for Declaration, TypeDecl, TypeDecl::Data |
---|
21 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, StmtExpr ... |
---|
22 | #include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only) |
---|
23 | #include "SynTree/Label.h" // for Label, noLabels |
---|
24 | #include "SynTree/Mutator.h" // for maybeMutate, mutateAll |
---|
25 | #include "SynTree/Statement.h" // for CatchStmt, CompoundStmt, ForStmt |
---|
26 | |
---|
27 | class TypeSubstitution; |
---|
28 | |
---|
29 | namespace GenPoly { |
---|
30 | PolyMutator::PolyMutator() : scopeTyVars( TypeDecl::Data{} ), env( 0 ) {} |
---|
31 | |
---|
32 | void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) { |
---|
33 | SemanticError errors; |
---|
34 | |
---|
35 | for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { |
---|
36 | if ( ! stmtsToAddAfter.empty() ) { |
---|
37 | statements.splice( i, stmtsToAddAfter ); |
---|
38 | } // if |
---|
39 | try { |
---|
40 | *i = (*i)->acceptMutator( *this ); |
---|
41 | } catch ( SemanticError &e ) { |
---|
42 | errors.append( e ); |
---|
43 | } // try |
---|
44 | if ( ! stmtsToAdd.empty() ) { |
---|
45 | statements.splice( i, stmtsToAdd ); |
---|
46 | } // if |
---|
47 | } // for |
---|
48 | if ( ! stmtsToAddAfter.empty() ) { |
---|
49 | statements.splice( statements.end(), stmtsToAddAfter ); |
---|
50 | } // if |
---|
51 | if ( ! errors.isEmpty() ) { |
---|
52 | throw errors; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | Statement * PolyMutator::mutateStatement( Statement *stmt ) { |
---|
57 | // don't want statements from outer CompoundStmts to be added to this CompoundStmt |
---|
58 | ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); |
---|
59 | ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); |
---|
60 | ValueGuard< TypeSubstitution * > oldEnv( env ); |
---|
61 | stmtsToAdd.clear(); |
---|
62 | stmtsToAddAfter.clear(); |
---|
63 | |
---|
64 | Statement *newStmt = maybeMutate( stmt, *this ); |
---|
65 | if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { |
---|
66 | CompoundStmt *compound = new CompoundStmt( noLabels ); |
---|
67 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); |
---|
68 | compound->get_kids().push_back( newStmt ); |
---|
69 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); |
---|
70 | // doEndScope(); |
---|
71 | return compound; |
---|
72 | } else { |
---|
73 | return newStmt; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | Expression * PolyMutator::mutateExpression( Expression *expr ) { |
---|
78 | if ( expr ) { |
---|
79 | if ( expr->get_env() ) { |
---|
80 | env = expr->get_env(); |
---|
81 | } |
---|
82 | // xxx - should env be cloned (or moved) onto the result of the mutate? |
---|
83 | return expr->acceptMutator( *this ); |
---|
84 | } else { |
---|
85 | return expr; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | CompoundStmt * PolyMutator::mutate(CompoundStmt *compoundStmt) { |
---|
90 | doBeginScope(); |
---|
91 | mutateStatementList( compoundStmt->get_kids() ); |
---|
92 | doEndScope(); |
---|
93 | return compoundStmt; |
---|
94 | } |
---|
95 | |
---|
96 | Statement * PolyMutator::mutate(IfStmt *ifStmt) { |
---|
97 | ifStmt->set_condition( mutateExpression( ifStmt->get_condition() ) ); |
---|
98 | ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) ); |
---|
99 | ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) ); |
---|
100 | return ifStmt; |
---|
101 | } |
---|
102 | |
---|
103 | Statement * PolyMutator::mutate(WhileStmt *whileStmt) { |
---|
104 | whileStmt->set_condition( mutateExpression( whileStmt->get_condition() ) ); |
---|
105 | whileStmt->set_body( mutateStatement( whileStmt->get_body() ) ); |
---|
106 | return whileStmt; |
---|
107 | } |
---|
108 | |
---|
109 | Statement * PolyMutator::mutate(ForStmt *forStmt) { |
---|
110 | mutateAll( forStmt->get_initialization(), *this ); |
---|
111 | forStmt->set_condition( mutateExpression( forStmt->get_condition() ) ); |
---|
112 | forStmt->set_increment( mutateExpression( forStmt->get_increment() ) ); |
---|
113 | forStmt->set_body( mutateStatement( forStmt->get_body() ) ); |
---|
114 | return forStmt; |
---|
115 | } |
---|
116 | |
---|
117 | Statement * PolyMutator::mutate(SwitchStmt *switchStmt) { |
---|
118 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); |
---|
119 | mutateStatementList( switchStmt->get_statements() ); |
---|
120 | return switchStmt; |
---|
121 | } |
---|
122 | |
---|
123 | Statement * PolyMutator::mutate(CaseStmt *caseStmt) { |
---|
124 | caseStmt->set_condition( mutateExpression( caseStmt->get_condition() ) ); |
---|
125 | mutateStatementList( caseStmt->get_statements() ); |
---|
126 | return caseStmt; |
---|
127 | } |
---|
128 | |
---|
129 | Statement * PolyMutator::mutate(TryStmt *tryStmt) { |
---|
130 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
131 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
132 | tryStmt->set_finally( maybeMutate( tryStmt->get_finally(), *this ) ); |
---|
133 | return tryStmt; |
---|
134 | } |
---|
135 | |
---|
136 | Statement * PolyMutator::mutate(CatchStmt *cathStmt) { |
---|
137 | cathStmt->set_body( mutateStatement( cathStmt->get_body() ) ); |
---|
138 | cathStmt->set_cond( maybeMutate( cathStmt->get_cond(), *this ) ); |
---|
139 | cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) ); |
---|
140 | return cathStmt; |
---|
141 | } |
---|
142 | |
---|
143 | Statement * PolyMutator::mutate(ReturnStmt *retStmt) { |
---|
144 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); |
---|
145 | return retStmt; |
---|
146 | } |
---|
147 | |
---|
148 | Statement * PolyMutator::mutate(ExprStmt *exprStmt) { |
---|
149 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); |
---|
150 | return exprStmt; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) { |
---|
155 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { |
---|
156 | *i = mutateExpression( *i ); |
---|
157 | } // for |
---|
158 | return untypedExpr; |
---|
159 | } |
---|
160 | |
---|
161 | Expression *PolyMutator::mutate( StmtExpr * stmtExpr ) { |
---|
162 | // don't want statements from outer CompoundStmts to be added to this StmtExpr |
---|
163 | ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); |
---|
164 | ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); |
---|
165 | ValueGuard< TypeSubstitution * > oldEnv( env ); |
---|
166 | |
---|
167 | // xxx - not sure if this is needed, along with appropriate reset, but I don't think so... |
---|
168 | // ValueGuard< TyVarMap > oldScopeTyVars( scopeTyVars ); |
---|
169 | |
---|
170 | stmtsToAdd.clear(); |
---|
171 | stmtsToAddAfter.clear(); |
---|
172 | // scopeTyVars.clear(); |
---|
173 | |
---|
174 | return Parent::mutate( stmtExpr ); |
---|
175 | } |
---|
176 | |
---|
177 | Initializer *PolyMutator::mutate( SingleInit *singleInit ) { |
---|
178 | singleInit->set_value( mutateExpression( singleInit->get_value() ) ); |
---|
179 | return singleInit; |
---|
180 | } |
---|
181 | } // namespace GenPoly |
---|
182 | |
---|
183 | // Local Variables: // |
---|
184 | // tab-width: 4 // |
---|
185 | // mode: c++ // |
---|
186 | // compile-command: "make install" // |
---|
187 | // End: // |
---|