source: src/SynTree/Statement.cc @ a40d503

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 a40d503 was 3ca540f, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'master' into with-statement

  • 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( const 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( Expression *expr ) : Statement(), 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( bool voltile, Expression *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement(), 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( Label target, Type type ) throw ( SemanticError ) :
99        Statement(), 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( Expression *computedTarget, Type type ) throw ( SemanticError ) :
107        Statement(), 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( Expression *expr ) : Statement(), 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( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
138        Statement(), 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( Expression * condition, const std::list<Statement *> &statements ):
179        Statement(), 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( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :
204        Statement(), 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( const std::list<Label> & labels, std::list<Statement *> stmts ) {
219        CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
220        stmt->labels = labels;
221        return stmt;
222}
223
224void CaseStmt::print( std::ostream &os, Indenter indent ) const {
225        if ( isDefault() ) os << "Default ";
226        else {
227                os << "Case ";
228                condition->print( os, indent );
229        } // if
230        os << endl;
231
232        for ( Statement * stmt : stmts ) {
233                stmt->print( os, indent+1 );
234        }
235}
236
237WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):
238        Statement(), condition( condition), body( body), isDoWhile( isDoWhile) {
239}
240
241WhileStmt::WhileStmt( const WhileStmt & other ):
242        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
243}
244
245WhileStmt::~WhileStmt() {
246        delete body;
247        delete condition;
248}
249
250void WhileStmt::print( std::ostream &os, Indenter indent ) const {
251        os << "While on condition: " << endl ;
252        condition->print( os, indent+1 );
253
254        os << indent << "... with body: " << endl;
255
256        if ( body != 0 ) body->print( os, indent+1 );
257}
258
259ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
260        Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
261}
262
263ForStmt::ForStmt( const ForStmt & other ):
264        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
265                cloneAll( other.initialization, initialization );
266
267}
268
269ForStmt::~ForStmt() {
270        deleteAll( initialization );
271        delete condition;
272        delete increment;
273        delete body;
274}
275
276void ForStmt::print( std::ostream &os, Indenter indent ) const {
277        Statement::print( os, indent ); // print labels
278
279        os << "For Statement" << endl;
280
281        if ( ! initialization.empty() ) {
282                os << indent << "... initialization: \n";
283                for ( Statement * stmt : initialization ) {
284                        os << indent+1;
285                        stmt->print( os, indent+1 );
286                }
287        }
288
289        if ( condition != nullptr ) {
290                os << indent << "... condition: \n" << indent+1;
291                condition->print( os, indent+1 );
292        }
293
294        if ( increment != nullptr ) {
295                os << "\n" << indent << "... increment: \n" << indent+1;
296                increment->print( os, indent+1 );
297        }
298
299        if ( body != 0 ) {
300                os << "\n" << indent << "... with body: \n" << indent+1;
301                body->print( os, indent+1 );
302        }
303        os << endl;
304}
305
306ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
307                Statement(), kind(kind), expr(expr), target(target)     {
308        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
309}
310
311ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
312        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
313}
314
315ThrowStmt::~ThrowStmt() {
316        delete expr;
317        delete target;
318}
319
320void ThrowStmt::print( std::ostream &os, Indenter indent) const {
321        if ( target ) os << "Non-Local ";
322        os << "Throw Statement, raising: ";
323        expr->print(os, indent+1);
324        if ( target ) {
325                os << "... at: ";
326                target->print(os, indent+1);
327        }
328}
329
330TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
331        Statement(), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
332}
333
334TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
335        cloneAll( other.handlers, handlers );
336}
337
338TryStmt::~TryStmt() {
339        delete block;
340        deleteAll( handlers );
341        delete finallyBlock;
342}
343
344void TryStmt::print( std::ostream &os, Indenter indent ) const {
345        os << "Try Statement" << endl;
346        os << indent << "... with block:" << endl << indent+1;
347        block->print( os, indent+1 );
348
349        // handlers
350        os << indent << "... and handlers:" << endl;
351        for ( const CatchStmt * stmt : handlers ) {
352                os << indent+1;
353                stmt->print( os, indent+1 );
354        }
355
356        // finally block
357        if ( finallyBlock != 0 ) {
358                os << indent << "... and finally:" << endl << indent+1;
359                finallyBlock->print( os, indent+1 );
360        } // if
361}
362
363CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
364        Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
365                assertf( decl, "Catch clause must have a declaration." );
366}
367
368CatchStmt::CatchStmt( const CatchStmt & other ) :
369        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
370}
371
372CatchStmt::~CatchStmt() {
373        delete decl;
374        delete body;
375}
376
377void CatchStmt::print( std::ostream &os, Indenter indent ) const {
378        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
379
380        os << indent << "... catching: ";
381        decl->printShort( os, indent+1 );
382        os << endl;
383
384        if ( cond ) {
385                os << indent << "... with conditional:" << endl << indent+1;
386                cond->print( os, indent+1 );
387        }
388
389        os << indent << "... with block:" << endl;
390        os << indent+1;
391        body->print( os, indent+1 );
392}
393
394
395FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
396}
397
398FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
399}
400
401FinallyStmt::~FinallyStmt() {
402        delete block;
403}
404
405void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
406        os << "Finally Statement" << endl;
407        os << indent << "... with block:" << endl << indent+1;
408        block->print( os, indent+1 );
409}
410
411WaitForStmt::WaitForStmt() : Statement() {
412        timeout.time      = nullptr;
413        timeout.statement = nullptr;
414        timeout.condition = nullptr;
415        orelse .statement = nullptr;
416        orelse .condition = nullptr;
417}
418
419WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
420        clauses.reserve( other.clauses.size() );
421        for( auto & ocl : other.clauses ) {
422                clauses.emplace_back();
423                clauses.back().target.function = ocl.target.function->clone();
424                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
425                clauses.back().statement = ocl.statement->clone();
426                clauses.back().condition = ocl.condition->clone();
427        }
428
429        timeout.time      = other.timeout.time     ->clone();
430        timeout.statement = other.timeout.statement->clone();
431        timeout.condition = other.timeout.condition->clone();
432        orelse .statement = other.orelse .statement->clone();
433        orelse .condition = other.orelse .condition->clone();
434}
435
436WaitForStmt::~WaitForStmt() {
437        for( auto & clause : clauses ) {
438                delete clause.target.function;
439                deleteAll( clause.target.arguments );
440                delete clause.statement;
441                delete clause.condition;
442        }
443
444        delete timeout.time;
445        delete timeout.statement;
446        delete timeout.condition;
447
448        delete orelse.statement;
449        delete orelse.condition;
450}
451
452void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
453        os << "Waitfor Statement" << endl;
454        os << indent << "... with block:" << endl << indent+1;
455        // block->print( os, indent + 4 );
456}
457
458
459WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
460WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
461        cloneAll( other.exprs, exprs );
462}
463WithStmt::~WithStmt() {
464        deleteAll( exprs );
465        delete stmt;
466}
467
468void WithStmt::print( std::ostream & os, Indenter indent ) const {
469        os << "With statement" << endl;
470        os << indent << "... with statement:" << endl << indent+1;
471        stmt->print( os, indent+1 );
472}
473
474
475NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
476}
477
478void NullStmt::print( std::ostream &os, Indenter ) const {
479        os << "Null Statement" << endl;
480}
481
482ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
483        assert( callStmt );
484}
485
486ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
487}
488
489ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
490        delete callStmt;
491}
492
493void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
494        os << "Implicit Ctor Dtor Statement" << endl;
495        os << indent << "... with Ctor/Dtor: ";
496        callStmt->print( os, indent+1);
497        os << endl;
498}
499
500// Local Variables: //
501// tab-width: 4 //
502// mode: c++ //
503// compile-command: "make install" //
504// End: //
Note: See TracBrowser for help on using the repository browser.