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 | // CaseRangeMutator.cc --
|
---|
8 | //
|
---|
9 | // Author : Rodolfo G. Esteves
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Sun Jul 31 00:14:16 2016
|
---|
13 | // Update Count : 29
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <list>
|
---|
17 | #include <cassert>
|
---|
18 | #include <cstdlib>
|
---|
19 | #include <iterator>
|
---|
20 |
|
---|
21 | #include "Common/utility.h"
|
---|
22 |
|
---|
23 | #include "SynTree/Statement.h"
|
---|
24 | #include "SynTree/Expression.h"
|
---|
25 | #include "SynTree/Constant.h"
|
---|
26 | #include "SynTree/Type.h"
|
---|
27 | #include "CaseRangeMutator.h"
|
---|
28 |
|
---|
29 | namespace ControlStruct {
|
---|
30 | Statement *CaseRangeMutator::mutate( SwitchStmt *switchStmt ) {
|
---|
31 | std::list< Statement * > &cases = switchStmt->get_branches();
|
---|
32 |
|
---|
33 | // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator
|
---|
34 | std::list< Statement * >::iterator i = cases.begin();
|
---|
35 | while ( i != cases.end() ) {
|
---|
36 | (*i)->acceptMutator( *this );
|
---|
37 |
|
---|
38 | if ( ! newCaseLabels.empty() ) {
|
---|
39 | std::list< Statement * > newCases;
|
---|
40 |
|
---|
41 | // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
|
---|
42 |
|
---|
43 | for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); j != newCaseLabels.end(); j++ ) {
|
---|
44 | std::list< Label > emptyLabels;
|
---|
45 | std::list< Statement * > emptyStmts;
|
---|
46 | newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
|
---|
47 | } // for
|
---|
48 |
|
---|
49 | if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * >( *i ) )
|
---|
50 | if ( ! currentCase->get_statements().empty() ) {
|
---|
51 | CaseStmt *lastCase = dynamic_cast< CaseStmt * >( newCases.back() );
|
---|
52 | if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
|
---|
53 | // transfer the statement block ( if any ) to the new list:
|
---|
54 | lastCase->set_statements( currentCase->get_statements() );
|
---|
55 | } // if
|
---|
56 | std::list< Statement * >::iterator j = i;
|
---|
57 | advance( j, 1 );
|
---|
58 | replace( cases, i, newCases );
|
---|
59 | i = j;
|
---|
60 | newCaseLabels.clear();
|
---|
61 | } else
|
---|
62 | i++;
|
---|
63 | } // while
|
---|
64 |
|
---|
65 | return switchStmt;
|
---|
66 | }
|
---|
67 |
|
---|
68 | Statement *CaseRangeMutator::mutate( CaseStmt *caseStmt ) {
|
---|
69 | if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) { // case list
|
---|
70 | assert( ! tcond->get_exprs().empty() );
|
---|
71 | for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ )
|
---|
72 | newCaseLabels.push_back( *i ); // do I need to clone them?
|
---|
73 | } // if
|
---|
74 |
|
---|
75 | std::list< Statement * > &stmts = caseStmt->get_statements();
|
---|
76 | mutateAll( stmts, *this );
|
---|
77 |
|
---|
78 | return caseStmt;
|
---|
79 | }
|
---|
80 | } // namespace ControlStruct
|
---|
81 |
|
---|
82 | // Local Variables: //
|
---|
83 | // tab-width: 4 //
|
---|
84 | // mode: c++ //
|
---|
85 | // compile-command: "make install" //
|
---|
86 | // End: //
|
---|