source: src/SynTree/Statement.cc @ 00c32e9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 00c32e9 was 4e06c1e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

changes for switch and choose statements

  • Property mode set to 100644
File size: 12.4 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 : Peter A. Buhr
12// Last Modified On : Tue Jul 12 17:52:32 2016
13// Update Count     : 55
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 << "Expression Statement:" << endl << std::string( indent + 2, ' ' );
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.empty() )
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 << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
113        if ( expr != 0 ) {
114                os << endl << string( indent+2, ' ' );
115                expr->print( os, indent + 2 );
116        }
117        os << endl;
118}
119
120IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
121        Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
122
123IfStmt::IfStmt( const IfStmt & other ) :
124        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}
125
126IfStmt::~IfStmt() {}
127
128void IfStmt::print( std::ostream &os, int indent ) const {
129        os << "If on condition: " << endl ;
130        os << string( indent+4, ' ' );
131        condition->print( os, indent + 4 );
132
133        os << string( indent+2, ' ' ) << "... then: " << endl;
134
135        os << string( indent+4, ' ' );
136        thenPart->print( os, indent + 4 );
137
138        if ( elsePart != 0 ) {
139                os << string( indent+2, ' ' ) << "... else: " << endl;
140                os << string( indent+4, ' ' );
141                elsePart->print( os, indent + 4 );
142        } // if
143}
144
145SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
146        Statement( _labels ), condition( _condition ), branches( _branches ) {
147}
148
149SwitchStmt::SwitchStmt( const SwitchStmt & other ):
150        Statement( other ), condition( maybeClone( other.condition ) ) {
151        cloneAll( other.branches, branches );
152}
153
154SwitchStmt::~SwitchStmt() {
155        delete condition;
156        // destroy branches
157}
158
159void SwitchStmt::add_case( CaseStmt *c ) {}
160
161void SwitchStmt::print( std::ostream &os, int indent ) const {
162        os << "Switch on condition: ";
163        condition->print( os );
164        os << endl;
165
166        // branches
167        std::list<Statement *>::const_iterator i;
168        for ( i = branches.begin(); i != branches.end(); i++)
169                (*i)->print( os, indent + 4 );
170
171        //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
172}
173
174CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
175        Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
176        if ( isDefault() && condition != 0 )
177                throw SemanticError("default with conditions");
178}
179
180CaseStmt::CaseStmt( const CaseStmt & other ) :
181        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
182        cloneAll( other.stmts, stmts );
183}
184
185CaseStmt::~CaseStmt() {
186        delete condition;
187}
188
189CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) {
190        return new CaseStmt( labels, 0, branches, true );
191}
192
193void CaseStmt::print( std::ostream &os, int indent ) const {
194        os << string( indent, ' ' );
195
196        if ( isDefault() )
197                os << "Default ";
198        else {
199                os << "Case ";
200                condition->print( os );
201        } // if
202
203        os << endl;
204
205        std::list<Statement *>::const_iterator i;
206        for ( i = stmts.begin(); i != stmts.end(); i++)
207                (*i )->print( os, indent + 4 );
208}
209
210WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
211        Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
212}
213
214WhileStmt::WhileStmt( const WhileStmt & other ):
215        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
216}
217
218WhileStmt::~WhileStmt() {
219        delete body;
220}
221
222void WhileStmt::print( std::ostream &os, int indent ) const {
223        os << "While on condition: " << endl ;
224        condition->print( os, indent + 4 );
225
226        os << string( indent, ' ' ) << ".... with body: " << endl;
227
228        if ( body != 0 ) body->print( os, indent + 4 );
229}
230
231ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
232        Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
233}
234
235ForStmt::ForStmt( const ForStmt & other ):
236        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
237                cloneAll( other.initialization, initialization );
238
239}
240
241ForStmt::~ForStmt() {
242        deleteAll( initialization );
243        delete condition;
244        delete increment;
245        delete body;
246}
247
248void ForStmt::print( std::ostream &os, int indent ) const {
249        os << "Labels: {";
250        for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
251                os << *it << ",";
252        }
253        os << "}" << endl;
254
255        os << string( indent, ' ' ) << "For Statement" << endl ;
256
257        os << string( indent + 2, ' ' ) << "initialization: \n";
258        for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
259                os << string( indent + 4, ' ' );
260                (*it)->print( os, indent + 4 );
261        }
262
263        os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
264        if ( condition != 0 ) {
265                os << string( indent + 4, ' ' );
266                condition->print( os, indent + 4 );
267        }
268
269        os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
270        if ( increment != 0 ) {
271                os << string( indent + 4, ' ' );
272                increment->print( os, indent + 4 );
273        }
274
275        os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
276        if ( body != 0 ) {
277                os << string( indent + 4, ' ' );
278                body->print( os, indent + 4 );
279        }
280
281        os << endl;
282}
283
284TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
285        Statement( labels ), block( tryBlock ),  handlers( _handlers ), finallyBlock( _finallyBlock ) {
286}
287
288TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
289        cloneAll( other.handlers, handlers );
290}
291
292TryStmt::~TryStmt() {
293        delete block;
294}
295
296void TryStmt::print( std::ostream &os, int indent ) const {
297        os << "Try Statement" << endl;
298        os << string( indent + 2, ' ' ) << "with block: " << endl;
299        block->print( os, indent + 4 );
300
301        // handlers
302        os << string( indent + 2, ' ' ) << "and handlers: " << endl;
303        for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
304                (*i )->print( os, indent + 4 );
305
306        // finally block
307        if ( finallyBlock != 0 ) {
308                os << string( indent + 2, ' ' ) << "Finally block: " << endl;
309                finallyBlock->print( os, indent + 4 );
310        } // if
311}
312
313CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) :
314        Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) {
315}
316
317CatchStmt::CatchStmt( const CatchStmt & other ) :
318        Statement( other ), decl ( maybeClone( other.decl ) ), body( maybeClone( other.body ) ), catchRest ( other.catchRest ) {
319}
320
321CatchStmt::~CatchStmt() {
322        delete decl;
323        delete body;
324}
325
326void CatchStmt::print( std::ostream &os, int indent ) const {
327        os << "Catch Statement" << endl;
328
329        os << string( indent, ' ' ) << "... catching" << endl;
330        if ( decl ) {
331                decl->printShort( os, indent + 4 );
332                os << endl;
333        } else if ( catchRest )
334                os << string( indent + 4 , ' ' ) << "the rest" << endl;
335        else
336                os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
337}
338
339
340FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
341        assert( labels.empty() ); // finally statement cannot be labeled
342}
343
344FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
345}
346
347FinallyStmt::~FinallyStmt() {
348        delete block;
349}
350
351void FinallyStmt::print( std::ostream &os, int indent ) const {
352        os << "Finally Statement" << endl;
353        os << string( indent + 2, ' ' ) << "with block: " << endl;
354        block->print( os, indent + 4 );
355}
356
357NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
358NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
359
360void NullStmt::print( std::ostream &os, int indent ) const {
361        os << "Null Statement" << endl ;
362}
363
364ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
365        assert( callStmt );
366}
367
368ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
369}
370
371ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
372        delete callStmt;
373}
374
375void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
376        os << "Implicit Ctor Dtor Statement" << endl;
377        os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
378        callStmt->print( os, indent + 2);
379        os << endl;
380}
381
382std::ostream & operator<<( std::ostream & out, Statement * statement ) {
383        statement->print( out );
384        return out;
385}
386
387// Local Variables: //
388// tab-width: 4 //
389// mode: c++ //
390// compile-command: "make install" //
391// End: //
Note: See TracBrowser for help on using the repository browser.