source: src/SynTree/Statement.cc @ 356189a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 356189a was 3be261a, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

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

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