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 | // Statement.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 : Mon Jun 29 17:37:10 2015
|
---|
13 | // Update Count : 22
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <functional>
|
---|
17 | #include <algorithm>
|
---|
18 | #include <iostream>
|
---|
19 | #include <list>
|
---|
20 | #include <cassert>
|
---|
21 |
|
---|
22 | #include "Statement.h"
|
---|
23 | #include "Expression.h"
|
---|
24 | #include "Declaration.h"
|
---|
25 | #include "Common/SemanticError.h"
|
---|
26 |
|
---|
27 | using std::string;
|
---|
28 | using std::endl;
|
---|
29 |
|
---|
30 | Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {}
|
---|
31 |
|
---|
32 | void Statement::print( std::ostream &, int indent ) const {}
|
---|
33 |
|
---|
34 | Statement::~Statement() {}
|
---|
35 |
|
---|
36 | ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {}
|
---|
37 |
|
---|
38 | ExprStmt::~ExprStmt() {}
|
---|
39 |
|
---|
40 | void ExprStmt::print( std::ostream &os, int indent ) const {
|
---|
41 | os << string( indent, ' ' ) << "Expression Statement:" << endl;
|
---|
42 | expr->print( os, indent + 2 );
|
---|
43 | }
|
---|
44 |
|
---|
45 | const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
|
---|
46 |
|
---|
47 | BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
|
---|
48 | Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) {
|
---|
49 | //actually this is a syntactic error signaled by the parser
|
---|
50 | if ( type == BranchStmt::Goto && target.size() == 0 )
|
---|
51 | throw SemanticError("goto without target");
|
---|
52 | }
|
---|
53 |
|
---|
54 | BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
|
---|
55 | Statement( labels ), computedTarget( _computedTarget ), type( _type ) {
|
---|
56 | if ( type != BranchStmt::Goto || computedTarget == 0 )
|
---|
57 | throw SemanticError("Computed target not valid in branch statement");
|
---|
58 | }
|
---|
59 |
|
---|
60 | void BranchStmt::print( std::ostream &os, int indent ) const {
|
---|
61 | os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
|
---|
62 | }
|
---|
63 |
|
---|
64 | ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {}
|
---|
65 |
|
---|
66 | ReturnStmt::~ReturnStmt() {
|
---|
67 | delete expr;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void ReturnStmt::print( std::ostream &os, int indent ) const {
|
---|
71 | os << std::string( indent, ' ' ) << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
|
---|
72 | if ( expr != 0 ) expr->print( os );
|
---|
73 | os << endl;
|
---|
74 | }
|
---|
75 |
|
---|
76 | IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
|
---|
77 | Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
|
---|
78 |
|
---|
79 | IfStmt::~IfStmt() {}
|
---|
80 |
|
---|
81 | void IfStmt::print( std::ostream &os, int indent ) const {
|
---|
82 | os << string( indent, ' ' ) << "If on condition: " << endl ;
|
---|
83 | condition->print( os, indent + 4 );
|
---|
84 |
|
---|
85 | os << string( indent, ' ' ) << ".... and branches: " << endl;
|
---|
86 |
|
---|
87 | thenPart->print( os, indent + 4 );
|
---|
88 |
|
---|
89 | if ( elsePart != 0 ) {
|
---|
90 | elsePart->print( os, indent + 4 );
|
---|
91 | } // if
|
---|
92 | }
|
---|
93 |
|
---|
94 | SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
|
---|
95 | Statement( _labels ), condition( _condition ), branches( _branches ) {
|
---|
96 | }
|
---|
97 |
|
---|
98 | SwitchStmt::~SwitchStmt() {
|
---|
99 | delete condition;
|
---|
100 | // destroy branches
|
---|
101 | }
|
---|
102 |
|
---|
103 | void SwitchStmt::add_case( CaseStmt *c ) {}
|
---|
104 |
|
---|
105 | void SwitchStmt::print( std::ostream &os, int indent ) const {
|
---|
106 | os << string( indent, ' ' ) << "Switch on condition: ";
|
---|
107 | condition->print( os );
|
---|
108 | os << endl;
|
---|
109 |
|
---|
110 | // branches
|
---|
111 | std::list<Statement *>::const_iterator i;
|
---|
112 | for ( i = branches.begin(); i != branches.end(); i++)
|
---|
113 | (*i)->print( os, indent + 4 );
|
---|
114 |
|
---|
115 | //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
|
---|
116 | }
|
---|
117 |
|
---|
118 | CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
|
---|
119 | Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
|
---|
120 | if ( isDefault() && condition != 0 )
|
---|
121 | throw SemanticError("default with conditions");
|
---|
122 | }
|
---|
123 |
|
---|
124 | CaseStmt::~CaseStmt() {
|
---|
125 | delete condition;
|
---|
126 | }
|
---|
127 |
|
---|
128 | CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) {
|
---|
129 | return new CaseStmt( labels, 0, branches, true );
|
---|
130 | }
|
---|
131 |
|
---|
132 | void CaseStmt::print( std::ostream &os, int indent ) const {
|
---|
133 | os << string( indent, ' ' );
|
---|
134 |
|
---|
135 | if ( isDefault() )
|
---|
136 | os << "Default ";
|
---|
137 | else {
|
---|
138 | os << "Case ";
|
---|
139 | condition->print( os );
|
---|
140 | } // if
|
---|
141 |
|
---|
142 | os << endl;
|
---|
143 |
|
---|
144 | std::list<Statement *>::const_iterator i;
|
---|
145 | for ( i = stmts.begin(); i != stmts.end(); i++)
|
---|
146 | (*i )->print( os, indent + 4 );
|
---|
147 | }
|
---|
148 |
|
---|
149 | //ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {}
|
---|
150 | ChooseStmt::ChooseStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
|
---|
151 | Statement( _labels ), condition( _condition ), branches( _branches ) {
|
---|
152 | }
|
---|
153 |
|
---|
154 | ChooseStmt::~ChooseStmt() {
|
---|
155 | delete condition;
|
---|
156 | }
|
---|
157 |
|
---|
158 | void ChooseStmt::add_case( CaseStmt *c ) {}
|
---|
159 |
|
---|
160 | void ChooseStmt::print( std::ostream &os, int indent ) const {
|
---|
161 | os << string( indent, ' ' ) << "Choose on condition: ";
|
---|
162 | condition->print( os );
|
---|
163 | os << endl;
|
---|
164 |
|
---|
165 | // branches
|
---|
166 | std::list<Statement *>::const_iterator i;
|
---|
167 | for ( i = branches.begin(); i != branches.end(); i++)
|
---|
168 | (*i )->print( os, indent + 4 );
|
---|
169 |
|
---|
170 | //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
|
---|
171 | }
|
---|
172 |
|
---|
173 | void FallthruStmt::print( std::ostream &os, int indent ) const {
|
---|
174 | os << string( indent, ' ' ) << "Fall-through statement" << endl;
|
---|
175 | }
|
---|
176 |
|
---|
177 | WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
|
---|
178 | Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
|
---|
179 | }
|
---|
180 |
|
---|
181 | WhileStmt::~WhileStmt() {
|
---|
182 | delete body;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void WhileStmt::print( std::ostream &os, int indent ) const {
|
---|
186 | os << string( indent, ' ' ) << "While on condition: " << endl ;
|
---|
187 | condition->print( os, indent + 4 );
|
---|
188 |
|
---|
189 | os << string( indent, ' ' ) << ".... with body: " << endl;
|
---|
190 |
|
---|
191 | if ( body != 0 ) body->print( os, indent + 4 );
|
---|
192 | }
|
---|
193 |
|
---|
194 | ForStmt::ForStmt( std::list<Label> labels, Statement *initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
|
---|
195 | Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
|
---|
196 | }
|
---|
197 |
|
---|
198 | ForStmt::~ForStmt() {
|
---|
199 | delete initialization;
|
---|
200 | delete condition;
|
---|
201 | delete increment;
|
---|
202 | delete body;
|
---|
203 | }
|
---|
204 |
|
---|
205 | void ForStmt::print( std::ostream &os, int indent ) const {
|
---|
206 | os << string( indent, ' ' ) << "Labels: {";
|
---|
207 | for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
|
---|
208 | os << *it << ",";
|
---|
209 | }
|
---|
210 | os << "}" << endl;
|
---|
211 |
|
---|
212 | os << string( indent, ' ' ) << "For Statement" << endl ;
|
---|
213 |
|
---|
214 | os << string( indent + 2, ' ' ) << "initialization: \n";
|
---|
215 | if ( initialization != 0 )
|
---|
216 | initialization->print( os, indent + 4 );
|
---|
217 |
|
---|
218 | os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
|
---|
219 | if ( condition != 0 )
|
---|
220 | condition->print( os, indent + 4 );
|
---|
221 |
|
---|
222 | os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
|
---|
223 | if ( increment != 0 )
|
---|
224 | increment->print( os, indent + 4 );
|
---|
225 |
|
---|
226 | os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
|
---|
227 | if ( body != 0 )
|
---|
228 | body->print( os, indent + 4 );
|
---|
229 |
|
---|
230 | os << endl;
|
---|
231 | }
|
---|
232 |
|
---|
233 | TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
|
---|
234 | Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock ) {
|
---|
235 | }
|
---|
236 |
|
---|
237 | TryStmt::TryStmt( const TryStmt &other ) : Statement( other.labels ) {
|
---|
238 | block = other.block;
|
---|
239 | std::copy( other.handlers.begin(), other.handlers.end(), back_inserter( handlers ) );
|
---|
240 | finallyBlock = other.finallyBlock;
|
---|
241 | }
|
---|
242 |
|
---|
243 | TryStmt::~TryStmt() {
|
---|
244 | delete block;
|
---|
245 | }
|
---|
246 |
|
---|
247 | void TryStmt::print( std::ostream &os, int indent ) const {
|
---|
248 | os << string( indent, ' ' ) << "Try Statement" << endl;
|
---|
249 | os << string( indent + 2, ' ' ) << "with block: " << endl;
|
---|
250 | block->print( os, indent + 4 );
|
---|
251 |
|
---|
252 | // handlers
|
---|
253 | os << string( indent + 2, ' ' ) << "and handlers: " << endl;
|
---|
254 | for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
|
---|
255 | (*i )->print( os, indent + 4 );
|
---|
256 |
|
---|
257 | // finally block
|
---|
258 | if ( finallyBlock != 0 ) {
|
---|
259 | os << string( indent + 2, ' ' ) << "Finally block: " << endl;
|
---|
260 | finallyBlock->print( os, indent + 4 );
|
---|
261 | } // if
|
---|
262 | }
|
---|
263 |
|
---|
264 | CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) :
|
---|
265 | Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) {
|
---|
266 | }
|
---|
267 |
|
---|
268 | CatchStmt::~CatchStmt() {
|
---|
269 | delete decl;
|
---|
270 | delete body;
|
---|
271 | }
|
---|
272 |
|
---|
273 | void CatchStmt::print( std::ostream &os, int indent ) const {
|
---|
274 | os << string( indent, ' ' ) << "Catch Statement" << endl;
|
---|
275 |
|
---|
276 | os << string( indent, ' ' ) << "... catching" << endl;
|
---|
277 | if ( decl ) {
|
---|
278 | decl->printShort( os, indent + 4 );
|
---|
279 | os << endl;
|
---|
280 | } else if ( catchRest )
|
---|
281 | os << string( indent + 4 , ' ' ) << "the rest" << endl;
|
---|
282 | else
|
---|
283 | os << string( indent + 4 , ' ' ) << ">>> Error: this catch clause must have a declaration <<<" << endl;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
|
---|
288 | assert( labels.empty() ); // finally statement cannot be labeled
|
---|
289 | }
|
---|
290 |
|
---|
291 | FinallyStmt::~FinallyStmt() {
|
---|
292 | delete block;
|
---|
293 | }
|
---|
294 |
|
---|
295 | void FinallyStmt::print( std::ostream &os, int indent ) const {
|
---|
296 | os << string( indent, ' ' ) << "Finally Statement" << endl;
|
---|
297 | os << string( indent + 2, ' ' ) << "with block: " << endl;
|
---|
298 | block->print( os, indent + 4 );
|
---|
299 | }
|
---|
300 |
|
---|
301 | NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
|
---|
302 | NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
|
---|
303 | NullStmt::~NullStmt() {}
|
---|
304 |
|
---|
305 | void NullStmt::print( std::ostream &os, int indent ) const {
|
---|
306 | os << string( indent, ' ' ) << "Null Statement" << endl ;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // Local Variables: //
|
---|
310 | // tab-width: 4 //
|
---|
311 | // mode: c++ //
|
---|
312 | // compile-command: "make install" //
|
---|
313 | // End: //
|
---|