source: src/GenPoly/PolyMutator.cc @ 00c32e9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 00c32e9 was 4e06c1e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

changes for switch and choose statements

  • Property mode set to 100644
File size: 4.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 : Tue Jul 12 17:39:32 2016
13// Update Count     : 12
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        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(CaseStmt *caseStmt) {
107                mutateStatementList( caseStmt->get_statements() );
108                caseStmt->set_condition(  mutateExpression( caseStmt->get_condition() ) );
109                return caseStmt;
110        }
111
112        Statement * PolyMutator::mutate(TryStmt *tryStmt) {
113                tryStmt->set_block(  maybeMutate( tryStmt->get_block(), *this ) );
114                mutateAll( tryStmt->get_catchers(), *this );
115                return tryStmt;
116        }
117
118        Statement * PolyMutator::mutate(CatchStmt *cathStmt) {
119                cathStmt->set_body(  mutateStatement( cathStmt->get_body() ) );
120                cathStmt->set_decl(  maybeMutate( cathStmt->get_decl(), *this ) );
121                return cathStmt;
122        }
123
124        Statement * PolyMutator::mutate(ReturnStmt *retStmt) {
125                retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
126                return retStmt;
127        }
128
129        Statement * PolyMutator::mutate(ExprStmt *exprStmt) {
130                exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) );
131                return exprStmt;
132        }
133
134
135        Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) {
136                for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) {
137                        *i = mutateExpression( *i );
138                } // for
139                return untypedExpr;
140        }
141
142
143        Initializer *PolyMutator::mutate( SingleInit *singleInit ) {
144                singleInit->set_value( mutateExpression( singleInit->get_value() ) );
145                return singleInit;
146        }
147
148} // namespace GenPoly
149
150// Local Variables: //
151// tab-width: 4 //
152// mode: c++ //
153// compile-command: "make install" //
154// End: //
Note: See TracBrowser for help on using the repository browser.