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 | // DeclMutator.cc -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Fri Nov 27 14:44:00 2015 |
---|
11 | // Last Modified By : Aaron B. Moss |
---|
12 | // Last Modified On : Fri Nov 27 14:44:00 2015 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "DeclMutator.h" |
---|
17 | |
---|
18 | #include "SynTree/Expression.h" |
---|
19 | #include "SynTree/Statement.h" |
---|
20 | |
---|
21 | namespace GenPoly { |
---|
22 | namespace { |
---|
23 | const std::list<Label> noLabels; |
---|
24 | } |
---|
25 | |
---|
26 | DeclMutator::DeclMutator() : Mutator(), declsToAdd(1) {} |
---|
27 | |
---|
28 | DeclMutator::~DeclMutator() {} |
---|
29 | |
---|
30 | void DeclMutator::mutateDeclarationList( std::list< Declaration* > &decls ) { |
---|
31 | for ( std::list< Declaration* >::iterator decl = decls.begin(); decl != decls.end(); ++decl ) { |
---|
32 | // run mutator on declaration |
---|
33 | *decl = maybeMutate( *decl, *this ); |
---|
34 | |
---|
35 | // splice in new declarations before current decl |
---|
36 | decls.splice( decl, declsToAdd.back() ); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | void DeclMutator::doBeginScope() { |
---|
41 | // add new decl list for inside of scope |
---|
42 | declsToAdd.resize( declsToAdd.size()+1 ); |
---|
43 | } |
---|
44 | |
---|
45 | void DeclMutator::doEndScope() { |
---|
46 | // splice any leftover declarations from this scope onto the containing scope |
---|
47 | std::vector< std::list< Declaration* > >::reverse_iterator back = declsToAdd.rbegin(); |
---|
48 | std::vector< std::list< Declaration* > >::reverse_iterator newBack = back + 1; |
---|
49 | newBack->splice( newBack->end(), *back ); |
---|
50 | declsToAdd.pop_back(); |
---|
51 | } |
---|
52 | |
---|
53 | Statement* DeclMutator::mutateStatement( Statement *stmt ) { |
---|
54 | // shunt over to compound statement handling if applicable |
---|
55 | CompoundStmt *compoundStmt = dynamic_cast< CompoundStmt* >(stmt); |
---|
56 | if ( compoundStmt ) return mutate( compoundStmt ); |
---|
57 | |
---|
58 | doBeginScope(); |
---|
59 | |
---|
60 | // run mutator on statement |
---|
61 | stmt = maybeMutate( stmt, *this ); |
---|
62 | // return if no declarations to add |
---|
63 | if ( declsToAdd.back().empty() ) return stmt; |
---|
64 | |
---|
65 | // otherwise add declarations to new compound statement |
---|
66 | CompoundStmt *compound = new CompoundStmt( noLabels ); |
---|
67 | for ( std::list< Declaration* >::iterator decl = declsToAdd.back().begin(); decl != declsToAdd.back().end(); ++decl ) { |
---|
68 | DeclStmt *declStmt = new DeclStmt( noLabels, *decl ); |
---|
69 | compound->get_kids().push_back( declStmt ); |
---|
70 | } |
---|
71 | declsToAdd.back().clear(); |
---|
72 | |
---|
73 | doEndScope(); |
---|
74 | |
---|
75 | // add mutated statement and return |
---|
76 | compound->get_kids().push_back( stmt ); |
---|
77 | return compound; |
---|
78 | } |
---|
79 | |
---|
80 | void DeclMutator::mutateStatementList( std::list< Statement* > &stmts ) { |
---|
81 | doBeginScope(); |
---|
82 | |
---|
83 | for ( std::list< Statement* >::iterator stmt = stmts.begin(); stmt != stmts.end(); ++stmt ) { |
---|
84 | // run mutator on statement |
---|
85 | *stmt = maybeMutate( *stmt, *this ); |
---|
86 | |
---|
87 | // add any new declarations before the statement |
---|
88 | for ( std::list< Declaration* >::iterator decl = declsToAdd.back().begin(); decl != declsToAdd.back().end(); ++decl ) { |
---|
89 | DeclStmt *declStmt = new DeclStmt( noLabels, *decl ); |
---|
90 | stmts.insert( stmt, declStmt ); |
---|
91 | } |
---|
92 | declsToAdd.back().clear(); |
---|
93 | } |
---|
94 | |
---|
95 | doEndScope(); |
---|
96 | } |
---|
97 | |
---|
98 | void DeclMutator::addDeclaration( Declaration *decl ) { |
---|
99 | declsToAdd.back().push_back( decl ); |
---|
100 | } |
---|
101 | |
---|
102 | CompoundStmt* DeclMutator::mutate(CompoundStmt *compoundStmt) { |
---|
103 | mutateStatementList( compoundStmt->get_kids() ); |
---|
104 | return compoundStmt; |
---|
105 | } |
---|
106 | |
---|
107 | Statement* DeclMutator::mutate(IfStmt *ifStmt) { |
---|
108 | ifStmt->set_condition( maybeMutate( ifStmt->get_condition(), *this ) ); |
---|
109 | ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) ); |
---|
110 | ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) ); |
---|
111 | return ifStmt; |
---|
112 | } |
---|
113 | |
---|
114 | Statement* DeclMutator::mutate(WhileStmt *whileStmt) { |
---|
115 | whileStmt->set_condition( maybeMutate( whileStmt->get_condition(), *this ) ); |
---|
116 | whileStmt->set_body( mutateStatement( whileStmt->get_body() ) ); |
---|
117 | return whileStmt; |
---|
118 | } |
---|
119 | |
---|
120 | Statement* DeclMutator::mutate(ForStmt *forStmt) { |
---|
121 | mutateAll( forStmt->get_initialization(), *this ); |
---|
122 | forStmt->set_condition( maybeMutate( forStmt->get_condition(), *this ) ); |
---|
123 | forStmt->set_increment( maybeMutate( forStmt->get_increment(), *this ) ); |
---|
124 | forStmt->set_body( mutateStatement( forStmt->get_body() ) ); |
---|
125 | return forStmt; |
---|
126 | } |
---|
127 | |
---|
128 | Statement* DeclMutator::mutate(SwitchStmt *switchStmt) { |
---|
129 | switchStmt->set_condition( maybeMutate( switchStmt->get_condition(), *this ) ); |
---|
130 | mutateAll( switchStmt->get_branches(), *this ); |
---|
131 | return switchStmt; |
---|
132 | } |
---|
133 | |
---|
134 | Statement* DeclMutator::mutate(ChooseStmt *chooseStmt) { |
---|
135 | chooseStmt->set_condition( maybeMutate( chooseStmt->get_condition(), *this ) ); |
---|
136 | mutateAll( chooseStmt->get_branches(), *this ); |
---|
137 | return chooseStmt; |
---|
138 | } |
---|
139 | |
---|
140 | Statement* DeclMutator::mutate(CaseStmt *caseStmt) { |
---|
141 | caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) ); |
---|
142 | mutateAll( caseStmt->get_statements(), *this ); |
---|
143 | return caseStmt; |
---|
144 | } |
---|
145 | |
---|
146 | Statement* DeclMutator::mutate(TryStmt *tryStmt) { |
---|
147 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
148 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
149 | tryStmt->set_finally( maybeMutate( tryStmt->get_finally(), *this ) ); |
---|
150 | return tryStmt; |
---|
151 | } |
---|
152 | |
---|
153 | Statement* DeclMutator::mutate(CatchStmt *catchStmt) { |
---|
154 | catchStmt->set_decl( maybeMutate( catchStmt->get_decl(), *this ) ); |
---|
155 | catchStmt->set_body( mutateStatement( catchStmt->get_body() ) ); |
---|
156 | return catchStmt; |
---|
157 | } |
---|
158 | } // namespace GenPoly |
---|
159 | |
---|
160 | // Local Variables: // |
---|
161 | // tab-width: 4 // |
---|
162 | // mode: c++ // |
---|
163 | // compile-command: "make install" // |
---|
164 | // End: // |
---|