source: src/SynTree/Statement.cc @ bb9d8e8

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 bb9d8e8 was 50377a4, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Refactor tree print code to use Indenter

  • Property mode set to 100644
File size: 15.0 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 : Sun Sep  3 20:46:44 2017
13// Update Count     : 68
14//
15
16#include "SynTree/Statement.h"
17
18#include <stddef.h>                // for NULL
19#include <cassert>                 // for assert, assertf
20#include <iostream>                // for operator<<, basic_ostream, endl
21#include <list>                    // for list, list<>::const_iterator, _Lis...
22#include <string>                  // for operator<<, string, char_traits
23
24#include "Common/SemanticError.h"  // for SemanticError
25#include "Common/utility.h"        // for maybeClone, cloneAll, deleteAll
26#include "Declaration.h"           // for Declaration
27#include "Expression.h"            // for Expression, ConstantExpr
28#include "Statement.h"             // for Statement, ForStmt, AsmStmt, Catch...
29#include "SynTree/Label.h"         // for Label, operator<<
30
31using std::string;
32using std::endl;
33
34Statement::Statement( std::list<Label> labels ) : labels( labels ) {}
35
36void Statement::print( std::ostream & os, Indenter ) const {
37        if ( ! labels.empty() ) {
38                os << "Labels: {";
39                for ( const Label & l : labels ) {
40                        os << l << ",";
41                }
42                os << "}" << endl;
43        }
44}
45
46Statement::~Statement() {}
47
48ExprStmt::ExprStmt( std::list<Label> labels, Expression *expr ) : Statement( labels ), expr( expr ) {}
49
50ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
51
52ExprStmt::~ExprStmt() {
53        delete expr;
54}
55
56void ExprStmt::print( std::ostream &os, Indenter indent ) const {
57        os << "Expression Statement:" << endl << indent+1;
58        expr->print( os, indent+1 );
59}
60
61
62AsmStmt::AsmStmt( std::list<Label> labels, bool voltile, Expression *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 ) {}
63
64AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
65  cloneAll( other.output, output );
66  cloneAll( other.input, input );
67  cloneAll( other.clobber, clobber );
68}
69
70AsmStmt::~AsmStmt() {
71        delete instruction;
72        deleteAll( output );
73        deleteAll( input );
74        deleteAll( clobber );
75}
76
77void AsmStmt::print( std::ostream &os, Indenter indent ) const {
78        os << "Assembler Statement:" << endl;
79        os << indent+1 << "instruction: " << endl << indent;
80        instruction->print( os, indent+1 );
81        if ( ! output.empty() ) {
82                os << endl << indent+1 << "output: " << endl;
83                printAll( output, os, indent+1 );
84        } // if
85        if ( ! input.empty() ) {
86                os << indent+1 << "input: " << endl;
87                printAll( input, os, indent+1 );
88        } // if
89        if ( ! clobber.empty() ) {
90                os << indent+1 << "clobber: " << endl;
91                printAll( clobber, os, indent+1 );
92        } // if
93}
94
95
96const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
97
98BranchStmt::BranchStmt( std::list<Label> labels, Label target, Type type ) throw ( SemanticError ) :
99        Statement( labels ), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
100        //actually this is a syntactic error signaled by the parser
101        if ( type == BranchStmt::Goto && target.empty() ) {
102                throw SemanticError("goto without target");
103        }
104}
105
106BranchStmt::BranchStmt( std::list<Label> labels, Expression *computedTarget, Type type ) throw ( SemanticError ) :
107        Statement( labels ), computedTarget( computedTarget ), type( type ) {
108        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
109                throw SemanticError("Computed target not valid in branch statement");
110        }
111}
112
113void BranchStmt::print( std::ostream &os, Indenter indent ) const {
114        os << "Branch (" << brType[type] << ")" << endl ;
115        if ( target != "" ) os << indent+1 << "with target: " << target << endl;
116        if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
117        if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
118}
119
120ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *expr ) : Statement( labels ), expr( expr ) {}
121
122ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
123
124ReturnStmt::~ReturnStmt() {
125        delete expr;
126}
127
128void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
129        os << "Return Statement, returning: ";
130        if ( expr != nullptr ) {
131                os << endl << indent+1;
132                expr->print( os, indent+1 );
133        }
134        os << endl;
135}
136
137IfStmt::IfStmt( std::list<Label> labels, Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
138        Statement( labels ), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
139
140IfStmt::IfStmt( const IfStmt & other ) :
141        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
142        cloneAll( other.initialization, initialization );
143}
144
145IfStmt::~IfStmt() {
146        deleteAll( initialization );
147        delete condition;
148        delete thenPart;
149        delete elsePart;
150}
151
152void IfStmt::print( std::ostream &os, Indenter indent ) const {
153        os << "If on condition: " << endl;
154        os << indent+1;
155        condition->print( os, indent+1 );
156
157        if ( !initialization.empty() ) {
158                os << indent << "... with initialization: \n";
159                for ( const Statement * stmt : initialization ) {
160                        os << indent+1;
161                        stmt->print( os, indent+1 );
162                }
163                os << endl;
164        }
165
166        os << indent << "... then: " << endl;
167
168        os << indent+1;
169        thenPart->print( os, indent+1 );
170
171        if ( elsePart != 0 ) {
172                os << indent << "... else: " << endl;
173                os << indent+1;
174                elsePart->print( os, indent+1 );
175        } // if
176}
177
178SwitchStmt::SwitchStmt( std::list<Label> labels, Expression * condition, const std::list<Statement *> &statements ):
179        Statement( labels ), condition( condition ), statements( statements ) {
180}
181
182SwitchStmt::SwitchStmt( const SwitchStmt & other ):
183        Statement( other ), condition( maybeClone( other.condition ) ) {
184        cloneAll( other.statements, statements );
185}
186
187SwitchStmt::~SwitchStmt() {
188        delete condition;
189        // destroy statements
190        deleteAll( statements );
191}
192
193void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
194        os << "Switch on condition: ";
195        condition->print( os );
196        os << endl;
197
198        for ( const Statement * stmt : statements ) {
199                stmt->print( os, indent+1 );
200        }
201}
202
203CaseStmt::CaseStmt( std::list<Label> labels, Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :
204        Statement( labels ), condition( condition ), stmts( statements ), _isDefault( deflt ) {
205        if ( isDefault() && condition != 0 ) throw SemanticError("default case with condition: ", condition);
206}
207
208CaseStmt::CaseStmt( const CaseStmt & other ) :
209        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
210        cloneAll( other.stmts, stmts );
211}
212
213CaseStmt::~CaseStmt() {
214        delete condition;
215        deleteAll( stmts );
216}
217
218CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> stmts ) {
219        return new CaseStmt( labels, 0, stmts, true );
220}
221
222void CaseStmt::print( std::ostream &os, Indenter indent ) const {
223        if ( isDefault() ) os << "Default ";
224        else {
225                os << "Case ";
226                condition->print( os, indent );
227        } // if
228        os << endl;
229
230        for ( Statement * stmt : stmts ) {
231                stmt->print( os, indent+1 );
232        }
233}
234
235WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition, Statement *body, bool isDoWhile ):
236        Statement( labels ), condition( condition), body( body), isDoWhile( isDoWhile) {
237}
238
239WhileStmt::WhileStmt( const WhileStmt & other ):
240        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
241}
242
243WhileStmt::~WhileStmt() {
244        delete body;
245        delete condition;
246}
247
248void WhileStmt::print( std::ostream &os, Indenter indent ) const {
249        os << "While on condition: " << endl ;
250        condition->print( os, indent+1 );
251
252        os << indent << "... with body: " << endl;
253
254        if ( body != 0 ) body->print( os, indent+1 );
255}
256
257ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
258        Statement( labels ), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
259}
260
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
267ForStmt::~ForStmt() {
268        deleteAll( initialization );
269        delete condition;
270        delete increment;
271        delete body;
272}
273
274void ForStmt::print( std::ostream &os, Indenter indent ) const {
275        Statement::print( os, indent ); // print labels
276
277        os << "For Statement" << endl;
278
279        if ( ! initialization.empty() ) {
280                os << indent << "... initialization: \n";
281                for ( Statement * stmt : initialization ) {
282                        os << indent+1;
283                        stmt->print( os, indent+1 );
284                }
285        }
286
287        if ( condition != nullptr ) {
288                os << indent << "... condition: \n" << indent+1;
289                condition->print( os, indent+1 );
290        }
291
292        if ( increment != nullptr ) {
293                os << "\n" << indent << "... increment: \n" << indent+1;
294                increment->print( os, indent+1 );
295        }
296
297        if ( body != 0 ) {
298                os << "\n" << indent << "... with body: \n" << indent+1;
299                body->print( os, indent+1 );
300        }
301        os << endl;
302}
303
304ThrowStmt::ThrowStmt( std::list<Label> labels, Kind kind, Expression * expr, Expression * target ) :
305                Statement( labels ), kind(kind), expr(expr), target(target)     {
306        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
307}
308
309ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
310        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
311}
312
313ThrowStmt::~ThrowStmt() {
314        delete expr;
315        delete target;
316}
317
318void ThrowStmt::print( std::ostream &os, Indenter indent) const {
319        if ( target ) os << "Non-Local ";
320        os << "Throw Statement, raising: ";
321        expr->print(os, indent+1);
322        if ( target ) {
323                os << "... at: ";
324                target->print(os, indent+1);
325        }
326}
327
328TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
329        Statement( labels ), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
330}
331
332TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
333        cloneAll( other.handlers, handlers );
334}
335
336TryStmt::~TryStmt() {
337        delete block;
338        deleteAll( handlers );
339        delete finallyBlock;
340}
341
342void TryStmt::print( std::ostream &os, Indenter indent ) const {
343        os << "Try Statement" << endl;
344        os << indent << "... with block:" << endl << indent+1;
345        block->print( os, indent+1 );
346
347        // handlers
348        os << indent << "... and handlers:" << endl;
349        for ( const CatchStmt * stmt : handlers ) {
350                os << indent+1;
351                stmt->print( os, indent+1 );
352        }
353
354        // finally block
355        if ( finallyBlock != 0 ) {
356                os << indent << "... and finally:" << endl << indent+1;
357                finallyBlock->print( os, indent+1 );
358        } // if
359}
360
361CatchStmt::CatchStmt( std::list<Label> labels, Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
362        Statement( labels ), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
363                assertf( decl, "Catch clause must have a declaration." );
364}
365
366CatchStmt::CatchStmt( const CatchStmt & other ) :
367        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
368}
369
370CatchStmt::~CatchStmt() {
371        delete decl;
372        delete body;
373}
374
375void CatchStmt::print( std::ostream &os, Indenter indent ) const {
376        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
377
378        os << indent << "... catching: ";
379        decl->printShort( os, indent+1 );
380        os << endl;
381
382        if ( cond ) {
383                os << indent << "... with conditional:" << endl << indent+1;
384                cond->print( os, indent+1 );
385        }
386
387        os << indent << "... with block:" << endl;
388        os << indent+1;
389        body->print( os, indent+1 );
390}
391
392
393FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *block ) : Statement( labels ), block( block ) {
394        assert( labels.empty() ); // finally statement cannot be labeled
395}
396
397FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
398}
399
400FinallyStmt::~FinallyStmt() {
401        delete block;
402}
403
404void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
405        os << "Finally Statement" << endl;
406        os << indent << "... with block:" << endl << indent+1;
407        block->print( os, indent+1 );
408}
409
410WaitForStmt::WaitForStmt( std::list<Label> labels ) : Statement( labels ) {
411        timeout.time      = nullptr;
412        timeout.statement = nullptr;
413        timeout.condition = nullptr;
414        orelse .statement = nullptr;
415        orelse .condition = nullptr;
416}
417
418WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
419        clauses.reserve( other.clauses.size() );
420        for( auto & ocl : other.clauses ) {
421                clauses.emplace_back();
422                clauses.back().target.function = ocl.target.function->clone();
423                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
424                clauses.back().statement = ocl.statement->clone();
425                clauses.back().condition = ocl.condition->clone();
426        }
427
428        timeout.time      = other.timeout.time     ->clone();
429        timeout.statement = other.timeout.statement->clone();
430        timeout.condition = other.timeout.condition->clone();
431        orelse .statement = other.orelse .statement->clone();
432        orelse .condition = other.orelse .condition->clone();
433}
434
435WaitForStmt::~WaitForStmt() {
436        for( auto & clause : clauses ) {
437                delete clause.target.function;
438                deleteAll( clause.target.arguments );
439                delete clause.statement;
440                delete clause.condition;
441        }
442
443        delete timeout.time;
444        delete timeout.statement;
445        delete timeout.condition;
446
447        delete orelse.statement;
448        delete orelse.condition;
449}
450
451void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
452        os << "Waitfor Statement" << endl;
453        os << indent << "... with block:" << endl << indent+1;
454        // block->print( os, indent + 4 );
455}
456
457NullStmt::NullStmt( std::list<Label> labels ) : Statement( labels ) {}
458NullStmt::NullStmt() : Statement( std::list<Label>() ) {}
459
460void NullStmt::print( std::ostream &os, Indenter ) const {
461        os << "Null Statement" << endl;
462}
463
464ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
465        assert( callStmt );
466}
467
468ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
469}
470
471ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
472        delete callStmt;
473}
474
475void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
476        os << "Implicit Ctor Dtor Statement" << endl;
477        os << indent << "... with Ctor/Dtor: ";
478        callStmt->print( os, indent+1);
479        os << endl;
480}
481
482// Local Variables: //
483// tab-width: 4 //
484// mode: c++ //
485// compile-command: "make install" //
486// End: //
Note: See TracBrowser for help on using the repository browser.