source: src/GenPoly/DeclMutator.cc@ 9b18044

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 9b18044 was 08fc48f, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Big header cleaning pass - commit 1

  • Property mode set to 100644
File size: 6.5 KB
Line 
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 : Andrew Beach
12// Last Modified On : Thu Jun 22 13:49:00 2017
13// Update Count : 4
14//
15
16#include "DeclMutator.h"
17
18#include <memory> // for allocator_traits<>::value_type
19
20#include "Common/SemanticError.h" // for SemanticError
21#include "SynTree/Declaration.h" // for Declaration
22#include "SynTree/Expression.h" // for Expression
23#include "SynTree/Label.h" // for Label, noLabels
24#include "SynTree/Statement.h" // for CatchStmt, Statement, CompoundStmt
25
26namespace GenPoly {
27 DeclMutator::DeclMutator() : Mutator(), declsToAdd(1), declsToAddAfter(1) {}
28
29 DeclMutator::~DeclMutator() {}
30
31 void DeclMutator::mutateDeclarationList( std::list< Declaration* > &decls ) {
32 for ( std::list< Declaration* >::iterator decl = decls.begin(); ; ++decl ) {
33 // splice in new declarations after previous decl
34 decls.splice( decl, declsToAddAfter.back() );
35
36 if ( decl == decls.end() ) break;
37
38 // run mutator on declaration
39 *decl = maybeMutate( *decl, *this );
40
41 // splice in new declarations before current decl
42 decls.splice( decl, declsToAdd.back() );
43 }
44 }
45
46 void DeclMutator::doBeginScope() {
47 // add new decl lists for inside of scope
48 declsToAdd.resize( declsToAdd.size()+1 );
49 declsToAddAfter.resize( declsToAddAfter.size()+1 );
50 }
51
52 void DeclMutator::doEndScope() {
53 // splice any leftover declarations from this scope onto the containing scope
54 std::vector< std::list< Declaration* > >::reverse_iterator back = declsToAdd.rbegin();
55 std::vector< std::list< Declaration* > >::reverse_iterator newBack = back + 1;
56 newBack->splice( newBack->end(), *back );
57 declsToAdd.pop_back();
58
59 back = declsToAddAfter.rbegin();
60 newBack = back + 1;
61 newBack->splice( newBack->end(), *back );
62 declsToAddAfter.pop_back();
63 }
64
65 Statement* DeclMutator::mutateStatement( Statement *stmt ) {
66 // shunt over to compound statement handling if applicable
67 CompoundStmt *compoundStmt = dynamic_cast< CompoundStmt* >(stmt);
68 if ( compoundStmt ) return mutate( compoundStmt );
69
70 doBeginScope();
71
72 // run mutator on statement
73 stmt = maybeMutate( stmt, *this );
74 // return if no declarations to add
75 if ( declsToAdd.back().empty() && declsToAddAfter.back().empty() ) {
76 doEndScope();
77 return stmt;
78 }
79
80 // otherwise add declarations to new compound statement
81 CompoundStmt *compound = new CompoundStmt( noLabels );
82 for ( std::list< Declaration* >::iterator decl = declsToAdd.back().begin(); decl != declsToAdd.back().end(); ++decl ) {
83 DeclStmt *declStmt = new DeclStmt( noLabels, *decl );
84 compound->get_kids().push_back( declStmt );
85 }
86 declsToAdd.back().clear();
87
88 // add mutated statement
89 compound->get_kids().push_back( stmt );
90
91 // add declarations after to new compound statement
92 for ( std::list< Declaration* >::iterator decl = declsToAddAfter.back().begin(); decl != declsToAddAfter.back().end(); ++decl ) {
93 DeclStmt *declStmt = new DeclStmt( noLabels, *decl );
94 compound->get_kids().push_back( declStmt );
95 }
96 declsToAddAfter.back().clear();
97
98 doEndScope();
99 return compound;
100 }
101
102 void DeclMutator::mutateStatementList( std::list< Statement* > &stmts ) {
103 doBeginScope();
104
105
106 for ( std::list< Statement* >::iterator stmt = stmts.begin(); ; ++stmt ) {
107 // add any new declarations after the previous statement
108 for ( std::list< Declaration* >::iterator decl = declsToAddAfter.back().begin(); decl != declsToAddAfter.back().end(); ++decl ) {
109 DeclStmt *declStmt = new DeclStmt( noLabels, *decl );
110 stmts.insert( stmt, declStmt );
111 }
112 declsToAddAfter.back().clear();
113
114 if ( stmt == stmts.end() ) break;
115
116 // run mutator on statement
117 *stmt = maybeMutate( *stmt, *this );
118
119 // add any new declarations before the statement
120 for ( std::list< Declaration* >::iterator decl = declsToAdd.back().begin(); decl != declsToAdd.back().end(); ++decl ) {
121 DeclStmt *declStmt = new DeclStmt( noLabels, *decl );
122 stmts.insert( stmt, declStmt );
123 }
124 declsToAdd.back().clear();
125 }
126
127 doEndScope();
128 }
129
130 void DeclMutator::addDeclaration( Declaration *decl ) {
131 declsToAdd.back().push_back( decl );
132 }
133
134 void DeclMutator::addDeclarationAfter( Declaration *decl ) {
135 declsToAddAfter.back().push_back( decl );
136 }
137
138 CompoundStmt* DeclMutator::mutate(CompoundStmt *compoundStmt) {
139 mutateStatementList( compoundStmt->get_kids() );
140 return compoundStmt;
141 }
142
143 Statement* DeclMutator::mutate(IfStmt *ifStmt) {
144 ifStmt->set_condition( maybeMutate( ifStmt->get_condition(), *this ) );
145 ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) );
146 ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) );
147 return ifStmt;
148 }
149
150 Statement* DeclMutator::mutate(WhileStmt *whileStmt) {
151 whileStmt->set_condition( maybeMutate( whileStmt->get_condition(), *this ) );
152 whileStmt->set_body( mutateStatement( whileStmt->get_body() ) );
153 return whileStmt;
154 }
155
156 Statement* DeclMutator::mutate(ForStmt *forStmt) {
157 mutateAll( forStmt->get_initialization(), *this );
158 forStmt->set_condition( maybeMutate( forStmt->get_condition(), *this ) );
159 forStmt->set_increment( maybeMutate( forStmt->get_increment(), *this ) );
160 forStmt->set_body( mutateStatement( forStmt->get_body() ) );
161 return forStmt;
162 }
163
164 Statement* DeclMutator::mutate(SwitchStmt *switchStmt) {
165 switchStmt->set_condition( maybeMutate( switchStmt->get_condition(), *this ) );
166 mutateAll( switchStmt->get_statements(), *this );
167 return switchStmt;
168 }
169
170 Statement* DeclMutator::mutate(CaseStmt *caseStmt) {
171 caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
172 mutateAll( caseStmt->get_statements(), *this );
173 return caseStmt;
174 }
175
176 Statement* DeclMutator::mutate(TryStmt *tryStmt) {
177 tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) );
178 mutateAll( tryStmt->get_catchers(), *this );
179 tryStmt->set_finally( maybeMutate( tryStmt->get_finally(), *this ) );
180 return tryStmt;
181 }
182
183 Statement* DeclMutator::mutate(CatchStmt *catchStmt) {
184 catchStmt->set_decl( maybeMutate( catchStmt->get_decl(), *this ) );
185 catchStmt->set_cond( maybeMutate( catchStmt->get_cond(), *this ) );
186 catchStmt->set_body( mutateStatement( catchStmt->get_body() ) );
187 return catchStmt;
188 }
189} // namespace GenPoly
190
191// Local Variables: //
192// tab-width: 4 //
193// mode: c++ //
194// compile-command: "make install" //
195// End: //
Note: See TracBrowser for help on using the repository browser.