source: src/SynTree/Statement.cc@ 04cdd9b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 04cdd9b was ac71a86, checked in by Thierry Delisle <tdelisle@…>, 10 years ago

removed more memory leaks from the system

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