source: translator/GenPoly/PolyMutator.cc @ f7f6785

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since f7f6785 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * This file is part of the Cforall project
3 *
4 * $Id: PolyMutator.cc,v 1.7 2005/08/29 20:14:13 rcbilson Exp $
5 *
6 */
7
8#include "PolyMutator.h"
9#include "SynTree/Declaration.h"
10#include "SynTree/Type.h"
11#include "SynTree/Expression.h"
12#include "SynTree/Statement.h"
13#include "SynTree/Mutator.h"
14
15
16namespace GenPoly {
17
18namespace {
19const std::list<Label> noLabels;
20}
21
22PolyMutator::PolyMutator()
23  : env( 0 )
24{
25}
26
27void
28PolyMutator::mutateStatementList( std::list< Statement* > &statements )
29{
30  for( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
31    if( !stmtsToAddAfter.empty() ) {
32      statements.splice( i, stmtsToAddAfter );
33    }
34    *i = (*i)->acceptMutator( *this );
35    if( !stmtsToAdd.empty() ) {
36      statements.splice( i, stmtsToAdd );
37    }
38  }
39  if( !stmtsToAddAfter.empty() ) {
40    statements.splice( statements.end(), stmtsToAddAfter );
41  }
42}
43
44Statement*
45PolyMutator::mutateStatement( Statement *stmt )
46{
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
60Expression*
61PolyMutator::mutateExpression( Expression *expr )
62{
63  if( expr ) {
64    if( expr->get_env() ) {
65      env = expr->get_env();
66    }
67    return expr->acceptMutator( *this );
68  } else {
69    return expr;
70  }
71}
72
73CompoundStmt*
74PolyMutator::mutate(CompoundStmt *compoundStmt)
75{
76  mutateStatementList( compoundStmt->get_kids() );
77  doEndScope();
78  return compoundStmt;
79}
80
81Statement*
82PolyMutator::mutate(IfStmt *ifStmt)
83{
84  ifStmt->set_thenPart(  mutateStatement( ifStmt->get_thenPart() ) );
85  ifStmt->set_elsePart(  mutateStatement( ifStmt->get_elsePart() ) );
86  ifStmt->set_condition(  mutateExpression( ifStmt->get_condition() ) );
87  return ifStmt;
88}
89
90Statement*
91PolyMutator::mutate(WhileStmt *whileStmt)
92{
93  whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) );
94  whileStmt->set_condition(  mutateExpression( whileStmt->get_condition() ) );
95  return whileStmt;
96}
97
98Statement*
99PolyMutator::mutate(ForStmt *forStmt)
100{
101  forStmt->set_body(  mutateStatement( forStmt->get_body() ) );
102  forStmt->set_initialization(  maybeMutate( forStmt->get_initialization(), *this ) );
103  forStmt->set_condition(  mutateExpression( forStmt->get_condition() ) );
104  forStmt->set_increment(  mutateExpression( forStmt->get_increment() ) );
105  return forStmt;
106}
107
108Statement*
109PolyMutator::mutate(SwitchStmt *switchStmt)
110{
111  mutateStatementList( switchStmt->get_branches() );
112  switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) );
113  return switchStmt;
114}
115
116Statement*
117PolyMutator::mutate(ChooseStmt *switchStmt)
118{
119  mutateStatementList( switchStmt->get_branches() );
120  switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) );
121  return switchStmt;
122}
123
124Statement*
125PolyMutator::mutate(CaseStmt *caseStmt)
126{
127  mutateStatementList( caseStmt->get_statements() );
128  caseStmt->set_condition(  mutateExpression( caseStmt->get_condition() ) );
129
130  return caseStmt;
131}
132
133Statement*
134PolyMutator::mutate(TryStmt *tryStmt)
135{
136  tryStmt->set_block(  maybeMutate( tryStmt->get_block(), *this ) );
137  mutateAll( tryStmt->get_catchers(), *this );
138 
139  return tryStmt;
140}
141
142Statement*
143PolyMutator::mutate(CatchStmt *cathStmt)
144{
145  cathStmt->set_body(  mutateStatement( cathStmt->get_body() ) );
146  cathStmt->set_decl(  maybeMutate( cathStmt->get_decl(), *this ) );
147  return cathStmt;
148}
149
150Statement* 
151PolyMutator::mutate(ReturnStmt *retStmt)
152{
153  retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
154  return retStmt;
155}
156
157Statement* 
158PolyMutator::mutate(ExprStmt *exprStmt)
159{
160  exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) );
161  return exprStmt;
162}
163
164
165Expression* 
166PolyMutator::mutate(UntypedExpr *untypedExpr)
167{
168  for( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) {
169    *i = mutateExpression( *i );
170  }
171  return untypedExpr;
172}
173 
174/* static class method */
175void 
176PolyMutator::makeTyVarMap( Type *type, TyVarMap &tyVarMap )
177{
178  for( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
179    assert( *tyVar );
180    tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
181  }
182  if( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
183    makeTyVarMap( pointer->get_base(), tyVarMap );
184  }
185}
186
187/* static class method */
188} // namespace GenPoly
Note: See TracBrowser for help on using the repository browser.