source: src/SynTree/Statement.cc @ d180746

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d180746 was 406a6e6, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Working on exception related readablility.

  • Property mode set to 100644
File size: 13.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 : Andrew Beach
12// Last Modified On : Mon Aug 14 12:26:00 2017
13// Update Count     : 65
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( __attribute__((unused)) std::ostream &, __attribute__((unused)) 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 ) : Statement( labels ), expr( _expr ) {}
104
105ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
106
107ReturnStmt::~ReturnStmt() {
108        delete expr;
109}
110
111void ReturnStmt::print( std::ostream &os, int indent ) const {
112        os <<  "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        delete condition;
128        delete thenPart;
129        delete elsePart;
130}
131
132void IfStmt::print( std::ostream &os, int indent ) const {
133        os << "If on condition: " << endl ;
134        os << string( indent+4, ' ' );
135        condition->print( os, indent + 4 );
136
137        os << string( indent+2, ' ' ) << "... then: " << endl;
138
139        os << string( indent+4, ' ' );
140        thenPart->print( os, indent + 4 );
141
142        if ( elsePart != 0 ) {
143                os << string( indent+2, ' ' ) << "... else: " << endl;
144                os << string( indent+4, ' ' );
145                elsePart->print( os, indent + 4 );
146        } // if
147}
148
149SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_statements ):
150        Statement( _labels ), condition( _condition ), statements( _statements ) {
151}
152
153SwitchStmt::SwitchStmt( const SwitchStmt & other ):
154        Statement( other ), condition( maybeClone( other.condition ) ) {
155        cloneAll( other.statements, statements );
156}
157
158SwitchStmt::~SwitchStmt() {
159        delete condition;
160        // destroy statements
161        deleteAll( statements );
162}
163
164void SwitchStmt::print( std::ostream &os, int indent ) const {
165        os << "Switch on condition: ";
166        condition->print( os );
167        os << endl;
168
169        // statements
170        std::list<Statement *>::const_iterator i;
171        for ( i = statements.begin(); i != statements.end(); i++)
172                (*i)->print( os, indent + 4 );
173
174        //for_each( statements.begin(), statements.end(), mem_fun( bind1st(&Statement::print ), os ));
175}
176
177CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
178        Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
179        if ( isDefault() && condition != 0 )
180                throw SemanticError("default with conditions");
181}
182
183CaseStmt::CaseStmt( const CaseStmt & other ) :
184        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
185        cloneAll( other.stmts, stmts );
186}
187
188CaseStmt::~CaseStmt() {
189        delete condition;
190        deleteAll( stmts );
191}
192
193CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> stmts ) {
194        return new CaseStmt( labels, 0, stmts, true );
195}
196
197void CaseStmt::print( std::ostream &os, int indent ) const {
198        os << string( indent, ' ' );
199
200        if ( isDefault() )
201                os << "Default ";
202        else {
203                os << "Case ";
204                condition->print( os );
205        } // if
206
207        os << endl;
208
209        std::list<Statement *>::const_iterator i;
210        for ( i = stmts.begin(); i != stmts.end(); i++)
211                (*i )->print( os, indent + 4 );
212}
213
214WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
215        Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
216}
217
218WhileStmt::WhileStmt( const WhileStmt & other ):
219        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
220}
221
222WhileStmt::~WhileStmt() {
223        delete body;
224        delete condition;
225}
226
227void WhileStmt::print( std::ostream &os, int indent ) const {
228        os << "While on condition: " << endl ;
229        condition->print( os, indent + 4 );
230
231        os << string( indent, ' ' ) << ".... with body: " << endl;
232
233        if ( body != 0 ) body->print( os, indent + 4 );
234}
235
236ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
237        Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
238}
239
240ForStmt::ForStmt( const ForStmt & other ):
241        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
242                cloneAll( other.initialization, initialization );
243
244}
245
246ForStmt::~ForStmt() {
247        deleteAll( initialization );
248        delete condition;
249        delete increment;
250        delete body;
251}
252
253void ForStmt::print( std::ostream &os, int indent ) const {
254        os << "Labels: {";
255        for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
256                os << *it << ",";
257        }
258        os << "}" << endl;
259
260        os << string( indent, ' ' ) << "For Statement" << endl ;
261
262        os << string( indent + 2, ' ' ) << "initialization: \n";
263        for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
264                os << string( indent + 4, ' ' );
265                (*it)->print( os, indent + 4 );
266        }
267
268        os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
269        if ( condition != 0 ) {
270                os << string( indent + 4, ' ' );
271                condition->print( os, indent + 4 );
272        }
273
274        os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
275        if ( increment != 0 ) {
276                os << string( indent + 4, ' ' );
277                increment->print( os, indent + 4 );
278        }
279
280        os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
281        if ( body != 0 ) {
282                os << string( indent + 4, ' ' );
283                body->print( os, indent + 4 );
284        }
285
286        os << endl;
287}
288
289ThrowStmt::ThrowStmt( std::list<Label> labels, Kind kind, Expression * expr, Expression * target ) :
290                Statement( labels ), kind(kind), expr(expr), target(target)     {
291        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
292}
293
294ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
295        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
296}
297
298ThrowStmt::~ThrowStmt() {
299        delete expr;
300        delete target;
301}
302
303void ThrowStmt::print( std::ostream &os, int indent) const {
304        if ( target ) {
305                os << "Non-Local ";
306        }
307        os << "Throw Statement, raising: ";
308        expr->print(os, indent + 4);
309        if ( target ) {
310                os << "At: ";
311                target->print(os, indent + 4);
312        }
313}
314
315TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<CatchStmt *> &_handlers, FinallyStmt *_finallyBlock ) :
316        Statement( labels ), block( tryBlock ),  handlers( _handlers ), finallyBlock( _finallyBlock ) {
317}
318
319TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
320        cloneAll( other.handlers, handlers );
321}
322
323TryStmt::~TryStmt() {
324        delete block;
325        deleteAll( handlers );
326        delete finallyBlock;
327}
328
329void TryStmt::print( std::ostream &os, int indent ) const {
330        os << "Try Statement" << endl;
331        os << string( indent + 2, ' ' ) << "with block:" << endl;
332        os << string( indent + 4, ' ' );
333        block->print( os, indent + 4 );
334
335        // handlers
336        os << string( indent + 2, ' ' ) << "and handlers:" << endl;
337        for ( std::list<CatchStmt *>::const_iterator i = handlers.begin(); i != handlers.end(); i++) {
338                os << string( indent + 4, ' ' );
339                (*i )->print( os, indent + 4 );
340        }
341
342        // finally block
343        if ( finallyBlock != 0 ) {
344                os << string( indent + 2, ' ' ) << "and finally:" << endl;
345                finallyBlock->print( os, indent + 4 );
346        } // if
347}
348
349CatchStmt::CatchStmt( std::list<Label> labels, Kind _kind, Declaration *_decl, Expression *_cond, Statement *_body ) :
350        Statement( labels ), kind ( _kind ), decl ( _decl ), cond ( _cond ), body( _body ) {
351}
352
353CatchStmt::CatchStmt( const CatchStmt & other ) :
354        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
355}
356
357CatchStmt::~CatchStmt() {
358        delete decl;
359        delete body;
360}
361
362void CatchStmt::print( std::ostream &os, int indent ) const {
363        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
364
365        os << string( indent + 2, ' ' ) << "... catching: ";
366        if ( decl ) {
367                decl->printShort( os, indent + 4 );
368                os << endl;
369        }
370        else
371                os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
372
373        if ( cond ) {
374                os << string( indent + 2, ' ' ) << "with conditional:" << endl;
375                os << string( indent + 4, ' ' );
376                cond->print( os, indent + 4 );
377        }
378        else
379                os << string( indent + 2, ' ' ) << "with no conditional" << endl;
380
381        os << string( indent + 2, ' ' ) << "with block:" << endl;
382        os << string( indent + 4, ' ' );
383        body->print( os, indent + 4 );
384}
385
386
387FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
388        assert( labels.empty() ); // finally statement cannot be labeled
389}
390
391FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
392}
393
394FinallyStmt::~FinallyStmt() {
395        delete block;
396}
397
398void FinallyStmt::print( std::ostream &os, int indent ) const {
399        os << "Finally Statement" << endl;
400        os << string( indent + 2, ' ' ) << "with block:" << endl;
401        os << string( indent + 4, ' ' );
402        block->print( os, indent + 4 );
403}
404
405NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
406NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
407
408void NullStmt::print( std::ostream &os, __attribute__((unused)) int indent ) const {
409        os << "Null Statement" << endl ;
410}
411
412ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
413        assert( callStmt );
414}
415
416ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
417}
418
419ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
420        delete callStmt;
421}
422
423void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
424        os << "Implicit Ctor Dtor Statement" << endl;
425        os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
426        callStmt->print( os, indent + 2);
427        os << endl;
428}
429
430std::ostream & operator<<( std::ostream & out, const Statement * statement ) {
431        if ( statement ) {
432                statement->print( out );
433        } else {
434                out << "nullptr";
435        }
436        return out;
437}
438
439// Local Variables: //
440// tab-width: 4 //
441// mode: c++ //
442// compile-command: "make install" //
443// End: //
Note: See TracBrowser for help on using the repository browser.