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 May 19 07:45:50 2015 |
---|
13 | // Update Count : 1 |
---|
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 | |
---|
23 | |
---|
24 | namespace GenPoly { |
---|
25 | namespace { |
---|
26 | const std::list<Label> noLabels; |
---|
27 | } |
---|
28 | |
---|
29 | PolyMutator::PolyMutator() : env( 0 ) { |
---|
30 | } |
---|
31 | |
---|
32 | void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) { |
---|
33 | for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { |
---|
34 | if ( ! stmtsToAddAfter.empty() ) { |
---|
35 | statements.splice( i, stmtsToAddAfter ); |
---|
36 | } // if |
---|
37 | *i = (*i)->acceptMutator( *this ); |
---|
38 | if ( ! stmtsToAdd.empty() ) { |
---|
39 | statements.splice( i, stmtsToAdd ); |
---|
40 | } // if |
---|
41 | } // for |
---|
42 | if ( ! stmtsToAddAfter.empty() ) { |
---|
43 | statements.splice( statements.end(), stmtsToAddAfter ); |
---|
44 | } // if |
---|
45 | } |
---|
46 | |
---|
47 | Statement * PolyMutator::mutateStatement( Statement *stmt ) { |
---|
48 | Statement *newStmt = maybeMutate( stmt, *this ); |
---|
49 | if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { |
---|
50 | CompoundStmt *compound = new CompoundStmt( noLabels ); |
---|
51 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); |
---|
52 | compound->get_kids().push_back( newStmt ); |
---|
53 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); |
---|
54 | // doEndScope(); |
---|
55 | return compound; |
---|
56 | } else { |
---|
57 | return newStmt; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | Expression * PolyMutator::mutateExpression( Expression *expr ) { |
---|
62 | if ( expr ) { |
---|
63 | if ( expr->get_env() ) { |
---|
64 | env = expr->get_env(); |
---|
65 | } |
---|
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 | forStmt->set_initialization( maybeMutate( 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(ChooseStmt *switchStmt) { |
---|
107 | mutateStatementList( switchStmt->get_branches() ); |
---|
108 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); |
---|
109 | return switchStmt; |
---|
110 | } |
---|
111 | |
---|
112 | Statement * PolyMutator::mutate(CaseStmt *caseStmt) { |
---|
113 | mutateStatementList( caseStmt->get_statements() ); |
---|
114 | caseStmt->set_condition( mutateExpression( caseStmt->get_condition() ) ); |
---|
115 | return caseStmt; |
---|
116 | } |
---|
117 | |
---|
118 | Statement * PolyMutator::mutate(TryStmt *tryStmt) { |
---|
119 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
120 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
121 | return tryStmt; |
---|
122 | } |
---|
123 | |
---|
124 | Statement * PolyMutator::mutate(CatchStmt *cathStmt) { |
---|
125 | cathStmt->set_body( mutateStatement( cathStmt->get_body() ) ); |
---|
126 | cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) ); |
---|
127 | return cathStmt; |
---|
128 | } |
---|
129 | |
---|
130 | Statement * PolyMutator::mutate(ReturnStmt *retStmt) { |
---|
131 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); |
---|
132 | return retStmt; |
---|
133 | } |
---|
134 | |
---|
135 | Statement * PolyMutator::mutate(ExprStmt *exprStmt) { |
---|
136 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); |
---|
137 | return exprStmt; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) { |
---|
142 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { |
---|
143 | *i = mutateExpression( *i ); |
---|
144 | } // for |
---|
145 | return untypedExpr; |
---|
146 | } |
---|
147 | |
---|
148 | /* static class method */ |
---|
149 | void PolyMutator::makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { |
---|
150 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { |
---|
151 | assert( *tyVar ); |
---|
152 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); |
---|
153 | } |
---|
154 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { |
---|
155 | makeTyVarMap( pointer->get_base(), tyVarMap ); |
---|
156 | } |
---|
157 | } |
---|
158 | } // namespace GenPoly |
---|
159 | |
---|
160 | // Local Variables: // |
---|
161 | // tab-width: 4 // |
---|
162 | // mode: c++ // |
---|
163 | // compile-command: "make install" // |
---|
164 | // End: // |
---|