source: src/SynTree/Statement.cc @ 4162aea9

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

fix MLE hack that puts a dangling break statement outside of a case statement in a switch

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