source: src/SynTree/Statement.cc@ ac1ed49

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since ac1ed49 was 3be261a, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

added copy constructors and destructors to many types that were missing them

  • Property mode set to 100644
File size: 12.7 KB
Line 
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 : Rob Schluntz
12// Last Modified On : Wed Dec 09 14:09:34 2015
13// Update Count : 54
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
27using std::string;
28using std::endl;
29
30Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {}
31
32void Statement::print( std::ostream &, int indent ) const {}
33
34Statement::~Statement() {}
35
36ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {}
37
38ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
39
40ExprStmt::~ExprStmt() {
41 delete expr;
42}
43
44void ExprStmt::print( std::ostream &os, int indent ) const {
45 os << string( indent, ' ' ) << "Expression Statement:" << endl;
46 expr->print( os, indent + 2 );
47}
48
49
50AsmStmt::AsmStmt( std::list<Label> labels, bool voltile, ConstantExpr *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement( labels ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {}
51
52AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
53 cloneAll( other.output, output );
54 cloneAll( other.input, input );
55 cloneAll( other.clobber, clobber );
56}
57
58AsmStmt::~AsmStmt() {
59 delete instruction;
60 deleteAll( output );
61 deleteAll( input );
62 deleteAll( clobber );
63}
64
65void AsmStmt::print( std::ostream &os, int indent ) const {
66 os << "Assembler Statement:" << endl;
67 os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' );
68 instruction->print( os, indent + 2 );
69 if ( ! output.empty() ) {
70 os << endl << std::string( indent, ' ' ) << "output: " << endl;
71 printAll( output, os, indent + 2 );
72 } // if
73 if ( ! input.empty() ) {
74 os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' );
75 printAll( input, os, indent + 2 );
76 } // if
77 if ( ! clobber.empty() ) {
78 os << std::string( indent, ' ' ) << "clobber: " << endl;
79 printAll( clobber, os, indent + 2 );
80 } // if
81}
82
83
84const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
85
86BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
87 Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) {
88 //actually this is a syntactic error signaled by the parser
89 if ( type == BranchStmt::Goto && target.size() == 0 )
90 throw SemanticError("goto without target");
91}
92
93BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
94 Statement( labels ), computedTarget( _computedTarget ), type( _type ) {
95 if ( type != BranchStmt::Goto || computedTarget == 0 )
96 throw SemanticError("Computed target not valid in branch statement");
97}
98
99void BranchStmt::print( std::ostream &os, int indent ) const {
100 os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
101}
102
103ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {}
104
105ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ), isThrow( other.isThrow ) {}
106
107ReturnStmt::~ReturnStmt() {
108 delete expr;
109}
110
111void ReturnStmt::print( std::ostream &os, int indent ) const {
112 os << std::string( indent, ' ' ) << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
113 if ( expr != 0 ) expr->print( os );
114 os << endl;
115}
116
117IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
118 Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
119
120IfStmt::IfStmt( const IfStmt & other ) :
121 Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}
122
123IfStmt::~IfStmt() {}
124
125void IfStmt::print( std::ostream &os, int indent ) const {
126 os << string( indent, ' ' ) << "If on condition: " << endl ;
127 condition->print( os, indent + 4 );
128
129 os << string( indent, ' ' ) << ".... and branches: " << endl;
130
131 thenPart->print( os, indent + 4 );
132
133 if ( elsePart != 0 ) {
134 elsePart->print( os, indent + 4 );
135 } // if
136}
137
138SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
139 Statement( _labels ), condition( _condition ), branches( _branches ) {
140}
141
142SwitchStmt::SwitchStmt( const SwitchStmt & other ):
143 Statement( other ), condition( maybeClone( other.condition ) ) {
144 cloneAll( other.branches, branches );
145}
146
147SwitchStmt::~SwitchStmt() {
148 delete condition;
149 // destroy branches
150}
151
152void SwitchStmt::add_case( CaseStmt *c ) {}
153
154void SwitchStmt::print( std::ostream &os, int indent ) const {
155 os << string( indent, ' ' ) << "Switch on condition: ";
156 condition->print( os );
157 os << endl;
158
159 // branches
160 std::list<Statement *>::const_iterator i;
161 for ( i = branches.begin(); i != branches.end(); i++)
162 (*i)->print( os, indent + 4 );
163
164 //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
165}
166
167CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
168 Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
169 if ( isDefault() && condition != 0 )
170 throw SemanticError("default with conditions");
171}
172
173CaseStmt::CaseStmt( const CaseStmt & other ) :
174 Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
175 cloneAll( other.stmts, stmts );
176}
177
178CaseStmt::~CaseStmt() {
179 delete condition;
180}
181
182CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) {
183 return new CaseStmt( labels, 0, branches, true );
184}
185
186void CaseStmt::print( std::ostream &os, int indent ) const {
187 os << string( indent, ' ' );
188
189 if ( isDefault() )
190 os << "Default ";
191 else {
192 os << "Case ";
193 condition->print( os );
194 } // if
195
196 os << endl;
197
198 std::list<Statement *>::const_iterator i;
199 for ( i = stmts.begin(); i != stmts.end(); i++)
200 (*i )->print( os, indent + 4 );
201}
202
203//ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {}
204ChooseStmt::ChooseStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
205 Statement( _labels ), condition( _condition ), branches( _branches ) {
206}
207
208ChooseStmt::ChooseStmt( const ChooseStmt & other ):
209 Statement( other ), condition( maybeClone( other.condition ) ) {
210 cloneAll( other.branches, branches );
211}
212
213ChooseStmt::~ChooseStmt() {
214 delete condition;
215}
216
217void ChooseStmt::add_case( CaseStmt *c ) {}
218
219void ChooseStmt::print( std::ostream &os, int indent ) const {
220 os << string( indent, ' ' ) << "Choose on condition: ";
221 condition->print( os );
222 os << endl;
223
224 // branches
225 std::list<Statement *>::const_iterator i;
226 for ( i = branches.begin(); i != branches.end(); i++)
227 (*i )->print( os, indent + 4 );
228
229 //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
230}
231
232void FallthruStmt::print( std::ostream &os, int indent ) const {
233 os << string( indent, ' ' ) << "Fall-through statement" << endl;
234}
235
236WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
237 Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
238}
239
240WhileStmt::WhileStmt( const WhileStmt & other ):
241 Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
242}
243
244WhileStmt::~WhileStmt() {
245 delete body;
246}
247
248void WhileStmt::print( std::ostream &os, int indent ) const {
249 os << string( indent, ' ' ) << "While on condition: " << endl ;
250 condition->print( os, indent + 4 );
251
252 os << string( indent, ' ' ) << ".... with body: " << endl;
253
254 if ( body != 0 ) body->print( os, indent + 4 );
255}
256
257ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
258 Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
259}
260
261ForStmt::ForStmt( const ForStmt & other ):
262 Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
263 cloneAll( other.initialization, initialization );
264
265}
266
267ForStmt::~ForStmt() {
268 deleteAll( initialization );
269 delete condition;
270 delete increment;
271 delete body;
272}
273
274void ForStmt::print( std::ostream &os, int indent ) const {
275 os << string( indent, ' ' ) << "Labels: {";
276 for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
277 os << *it << ",";
278 }
279 os << "}" << endl;
280
281 os << string( indent, ' ' ) << "For Statement" << endl ;
282
283 os << string( indent + 2, ' ' ) << "initialization: \n";
284 for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
285 (*it)->print( os, indent + 4 );
286 }
287
288 os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
289 if ( condition != 0 )
290 condition->print( os, indent + 4 );
291
292 os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
293 if ( increment != 0 )
294 increment->print( os, indent + 4 );
295
296 os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
297 if ( body != 0 )
298 body->print( os, indent + 4 );
299
300 os << endl;
301}
302
303TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
304 Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock ) {
305}
306
307TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
308 cloneAll( other.handlers, handlers );
309}
310
311TryStmt::~TryStmt() {
312 delete block;
313}
314
315void TryStmt::print( std::ostream &os, int indent ) const {
316 os << string( indent, ' ' ) << "Try Statement" << endl;
317 os << string( indent + 2, ' ' ) << "with block: " << endl;
318 block->print( os, indent + 4 );
319
320 // handlers
321 os << string( indent + 2, ' ' ) << "and handlers: " << endl;
322 for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
323 (*i )->print( os, indent + 4 );
324
325 // finally block
326 if ( finallyBlock != 0 ) {
327 os << string( indent + 2, ' ' ) << "Finally block: " << endl;
328 finallyBlock->print( os, indent + 4 );
329 } // if
330}
331
332CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) :
333 Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) {
334}
335
336CatchStmt::CatchStmt( const CatchStmt & other ) :
337 Statement( other ), decl ( maybeClone( other.decl ) ), body( maybeClone( other.body ) ), catchRest ( other.catchRest ) {
338}
339
340CatchStmt::~CatchStmt() {
341 delete decl;
342 delete body;
343}
344
345void CatchStmt::print( std::ostream &os, int indent ) const {
346 os << string( indent, ' ' ) << "Catch Statement" << endl;
347
348 os << string( indent, ' ' ) << "... catching" << endl;
349 if ( decl ) {
350 decl->printShort( os, indent + 4 );
351 os << endl;
352 } else if ( catchRest )
353 os << string( indent + 4 , ' ' ) << "the rest" << endl;
354 else
355 os << string( indent + 4 , ' ' ) << ">>> Error: this catch clause must have a declaration <<<" << endl;
356}
357
358
359FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
360 assert( labels.empty() ); // finally statement cannot be labeled
361}
362
363FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
364}
365
366FinallyStmt::~FinallyStmt() {
367 delete block;
368}
369
370void FinallyStmt::print( std::ostream &os, int indent ) const {
371 os << string( indent, ' ' ) << "Finally Statement" << endl;
372 os << string( indent + 2, ' ' ) << "with block: " << endl;
373 block->print( os, indent + 4 );
374}
375
376NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
377NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
378
379void NullStmt::print( std::ostream &os, int indent ) const {
380 os << string( indent, ' ' ) << "Null Statement" << endl ;
381}
382
383std::ostream & operator<<( std::ostream & out, Statement * statement ) {
384 statement->print( out );
385 return out;
386}
387
388// Local Variables: //
389// tab-width: 4 //
390// mode: c++ //
391// compile-command: "make install" //
392// End: //
Note: See TracBrowser for help on using the repository browser.