source: src/GenPoly/PolyMutator.cc @ 6ca154b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6ca154b was 45a4ea7, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

fix out-of-order traversal in PolyMutator?

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