1 | #include <list> |
---|
2 | #include <cassert> |
---|
3 | #include <cstdlib> |
---|
4 | #include <iterator> |
---|
5 | |
---|
6 | #include "utility.h" |
---|
7 | |
---|
8 | #include "SynTree/Statement.h" |
---|
9 | #include "SynTree/Expression.h" |
---|
10 | #include "SynTree/Constant.h" |
---|
11 | #include "SynTree/Type.h" |
---|
12 | #include "CaseRangeMutator.h" |
---|
13 | |
---|
14 | namespace ControlStruct { |
---|
15 | |
---|
16 | Statement* CaseRangeMutator::mutate(ChooseStmt *chooseStmt) |
---|
17 | { |
---|
18 | /* There shouldn't be any `choose' statements by now, throw an exception or something. */ |
---|
19 | throw( 0 ) ; /* FIXME */ |
---|
20 | } |
---|
21 | |
---|
22 | Statement* CaseRangeMutator::mutate(SwitchStmt *switchStmt) |
---|
23 | { |
---|
24 | std::list< Statement * > &cases = switchStmt->get_branches(); |
---|
25 | |
---|
26 | // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator |
---|
27 | std::list< Statement * >::iterator i = cases.begin(); |
---|
28 | while ( i != cases.end() ) { |
---|
29 | (*i)->acceptMutator( *this ); |
---|
30 | |
---|
31 | if ( ! newCaseLabels.empty() ) { |
---|
32 | std::list< Statement * > newCases; |
---|
33 | |
---|
34 | // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) ); |
---|
35 | |
---|
36 | for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); |
---|
37 | j != newCaseLabels.end(); j++ ) { |
---|
38 | std::list<Label> emptyLabels; |
---|
39 | std::list< Statement *> emptyStmts; |
---|
40 | newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) ); |
---|
41 | } |
---|
42 | |
---|
43 | if( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) ) |
---|
44 | if ( !currentCase->get_statements().empty() ) { |
---|
45 | CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() ); |
---|
46 | if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases |
---|
47 | // transfer the statement block (if any) to the new list: |
---|
48 | lastCase->set_statements( currentCase->get_statements() ); |
---|
49 | } |
---|
50 | std::list< Statement * >::iterator j = i; advance( j, 1 ); |
---|
51 | replace ( cases, i, newCases ); |
---|
52 | i = j; |
---|
53 | newCaseLabels.clear(); |
---|
54 | } else |
---|
55 | i++; |
---|
56 | } |
---|
57 | |
---|
58 | return switchStmt; |
---|
59 | } |
---|
60 | |
---|
61 | Statement* CaseRangeMutator::mutate(FallthruStmt *fallthruStmt) |
---|
62 | { |
---|
63 | //delete fallthruStmt; |
---|
64 | return new NullStmt(); |
---|
65 | } |
---|
66 | |
---|
67 | Statement* CaseRangeMutator::mutate(CaseStmt *caseStmt) |
---|
68 | { |
---|
69 | UntypedExpr *cond; |
---|
70 | if ( (cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) { |
---|
71 | NameExpr *nmfunc; |
---|
72 | if ( (nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) { |
---|
73 | if ( nmfunc->get_name() == std::string("Range") ) { |
---|
74 | assert( cond->get_args().size() == 2 ); |
---|
75 | std::list<Expression *>::iterator i = cond->get_args().begin(); |
---|
76 | Expression *lo = *i, *hi = *(++i); // "unnecessary" temporaries |
---|
77 | fillRange( lo, hi); |
---|
78 | } |
---|
79 | } |
---|
80 | } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) { |
---|
81 | // case list |
---|
82 | assert( ! tcond->get_exprs().empty() ); |
---|
83 | for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ ) |
---|
84 | newCaseLabels.push_back( *i ); // do I need to clone them? |
---|
85 | } |
---|
86 | |
---|
87 | std::list< Statement * > &stmts = caseStmt->get_statements(); |
---|
88 | mutateAll ( stmts, *this ); |
---|
89 | |
---|
90 | return caseStmt; |
---|
91 | } |
---|
92 | |
---|
93 | void CaseRangeMutator::fillRange(Expression *lo, Expression *hi) { |
---|
94 | // generate the actual range (and check for consistency) |
---|
95 | Constant *c_lo, *c_hi; |
---|
96 | ConstantExpr *ce_lo, *ce_hi; |
---|
97 | ce_lo = dynamic_cast< ConstantExpr * >( lo ); |
---|
98 | ce_hi = dynamic_cast< ConstantExpr * >( hi ); |
---|
99 | |
---|
100 | if ( ce_lo && ce_hi ) { |
---|
101 | c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant(); |
---|
102 | } /* else { |
---|
103 | if ( !ce_lo ) ; |
---|
104 | if ( !ce_hi ) ; |
---|
105 | } */ |
---|
106 | BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ), |
---|
107 | *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() ); |
---|
108 | |
---|
109 | if( !ty_lo || !ty_hi ) |
---|
110 | return; // one of them is not a constant |
---|
111 | |
---|
112 | |
---|
113 | switch( ty_lo->get_kind() ) { |
---|
114 | case BasicType::Char: |
---|
115 | case BasicType::UnsignedChar: |
---|
116 | switch( ty_hi->get_kind() ) |
---|
117 | { |
---|
118 | case BasicType::Char: |
---|
119 | case BasicType::UnsignedChar: |
---|
120 | // first case, they are both printable ASCII characters represented as 'x' |
---|
121 | if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) { |
---|
122 | char ch_lo = (c_lo->get_value())[1], ch_hi = (c_hi->get_value())[1]; |
---|
123 | |
---|
124 | if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; } |
---|
125 | |
---|
126 | for( char c = ch_lo; c <= ch_hi; c++ ){ |
---|
127 | Type::Qualifiers q; |
---|
128 | Constant cnst( new BasicType(q, BasicType::Char), |
---|
129 | std::string("'") + c + std::string("'") ); |
---|
130 | newCaseLabels.push_back( new ConstantExpr( cnst ) ); |
---|
131 | } |
---|
132 | |
---|
133 | return; |
---|
134 | } |
---|
135 | break; |
---|
136 | default: |
---|
137 | // error: incompatible constants |
---|
138 | break; |
---|
139 | } |
---|
140 | break; |
---|
141 | case BasicType::ShortSignedInt: |
---|
142 | case BasicType::ShortUnsignedInt: |
---|
143 | case BasicType::SignedInt: |
---|
144 | case BasicType::UnsignedInt: |
---|
145 | case BasicType::LongSignedInt: |
---|
146 | case BasicType::LongUnsignedInt: |
---|
147 | case BasicType::LongLongSignedInt: |
---|
148 | case BasicType::LongLongUnsignedInt: |
---|
149 | switch( ty_hi->get_kind() ) |
---|
150 | { |
---|
151 | case BasicType::ShortSignedInt: |
---|
152 | case BasicType::ShortUnsignedInt: |
---|
153 | case BasicType::SignedInt: |
---|
154 | case BasicType::UnsignedInt: |
---|
155 | case BasicType::LongSignedInt: |
---|
156 | case BasicType::LongUnsignedInt: |
---|
157 | case BasicType::LongLongSignedInt: |
---|
158 | case BasicType::LongLongUnsignedInt: { |
---|
159 | int i_lo = atoi(c_lo->get_value().c_str()), |
---|
160 | i_hi = atoi(c_hi->get_value().c_str()); |
---|
161 | |
---|
162 | if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; } |
---|
163 | |
---|
164 | for( int c = i_lo; c <= i_hi; c++ ){ |
---|
165 | Type::Qualifiers q; |
---|
166 | Constant cnst( new BasicType(q, ty_hi->get_kind()), // figure can't hurt (used to think in positives) |
---|
167 | toString< int >( c ) ); |
---|
168 | newCaseLabels.push_back( new ConstantExpr( cnst ) ); |
---|
169 | } |
---|
170 | |
---|
171 | return; |
---|
172 | } |
---|
173 | default: |
---|
174 | // error: incompatible constants |
---|
175 | break; |
---|
176 | } |
---|
177 | break; |
---|
178 | default: |
---|
179 | break; |
---|
180 | } |
---|
181 | |
---|
182 | /* End: */{ |
---|
183 | // invalid range, signal a warning (it still generates the two case labels) |
---|
184 | newCaseLabels.push_back( lo ); |
---|
185 | newCaseLabels.push_back( hi ); |
---|
186 | return; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | } // namespace ControlStruct |
---|