source: src/SynTree/Statement.cc @ 8d7bef2

new-envwith_gc
Last change on this file since 8d7bef2 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 7 years ago

First pass at delete removal

  • Property mode set to 100644
File size: 13.8 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//
[3be261a]7// Statement.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[6d49ea3]11// Last Modified By : Peter A. Buhr
[e612146c]12// Last Modified On : Sun Sep  3 20:46:44 2017
13// Update Count     : 68
[0dd3a2f]14//
15
[ea6332d]16#include "SynTree/Statement.h"
[51b7345]17
[ea6332d]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<<
[51b7345]30
31using std::string;
32using std::endl;
33
[ba3706f]34Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {}
[51b7345]35
[50377a4]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}
[51b7345]45
[ba3706f]46ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {}
[51b7345]47
[3be261a]48ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
49
[50377a4]50void ExprStmt::print( std::ostream &os, Indenter indent ) const {
51        os << "Expression Statement:" << endl << indent+1;
52        expr->print( os, indent+1 );
[3be261a]53}
[51b7345]54
[7f5566b]55
[ba3706f]56AsmStmt::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 ) {}
[7f5566b]57
[3be261a]58AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
59  cloneAll( other.output, output );
60  cloneAll( other.input, input );
61  cloneAll( other.clobber, clobber );
62}
63
[50377a4]64void AsmStmt::print( std::ostream &os, Indenter indent ) const {
[7f5566b]65        os << "Assembler Statement:" << endl;
[50377a4]66        os << indent+1 << "instruction: " << endl << indent;
67        instruction->print( os, indent+1 );
[7f5566b]68        if ( ! output.empty() ) {
[50377a4]69                os << endl << indent+1 << "output: " << endl;
70                printAll( output, os, indent+1 );
[3be261a]71        } // if
[7f5566b]72        if ( ! input.empty() ) {
[50377a4]73                os << indent+1 << "input: " << endl;
74                printAll( input, os, indent+1 );
[7f5566b]75        } // if
76        if ( ! clobber.empty() ) {
[50377a4]77                os << indent+1 << "clobber: " << endl;
78                printAll( clobber, os, indent+1 );
[7f5566b]79        } // if
[3be261a]80}
[7f5566b]81
82
[51b7345]83const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
84
[a16764a6]85BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
[ba3706f]86        Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
[0dd3a2f]87        //actually this is a syntactic error signaled by the parser
[b0dfbc4]88        if ( type == BranchStmt::Goto && target.empty() ) {
[a16764a6]89                SemanticError( target.get_statement()->location, "goto without target");
[b0dfbc4]90        }
[51b7345]91}
92
[a16764a6]93BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
[ba3706f]94        Statement(), computedTarget( computedTarget ), type( type ) {
[b0dfbc4]95        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
[a16764a6]96                SemanticError( computedTarget->location, "Computed target not valid in branch statement");
[b0dfbc4]97        }
[51b7345]98}
99
[50377a4]100void BranchStmt::print( std::ostream &os, Indenter indent ) const {
101        os << "Branch (" << brType[type] << ")" << endl ;
102        if ( target != "" ) os << indent+1 << "with target: " << target << endl;
103        if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
104        if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
[51b7345]105}
106
[ba3706f]107ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {}
[51b7345]108
[daf1af8]109ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
[3be261a]110
[50377a4]111void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
112        os << "Return Statement, returning: ";
113        if ( expr != nullptr ) {
114                os << endl << indent+1;
115                expr->print( os, indent+1 );
[89231bc]116        }
[0dd3a2f]117        os << endl;
[51b7345]118}
119
[ba3706f]120IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
121        Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
[51b7345]122
[3be261a]123IfStmt::IfStmt( const IfStmt & other ) :
[6d49ea3]124        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
125        cloneAll( other.initialization, initialization );
126}
[3be261a]127
[50377a4]128void IfStmt::print( std::ostream &os, Indenter indent ) const {
129        os << "If on condition: " << endl;
130        os << indent+1;
131        condition->print( os, indent+1 );
[51b7345]132
[6d49ea3]133        if ( !initialization.empty() ) {
[50377a4]134                os << indent << "... with initialization: \n";
135                for ( const Statement * stmt : initialization ) {
136                        os << indent+1;
137                        stmt->print( os, indent+1 );
[6d49ea3]138                }
139                os << endl;
140        }
141
[50377a4]142        os << indent << "... then: " << endl;
[51b7345]143
[50377a4]144        os << indent+1;
145        thenPart->print( os, indent+1 );
[51b7345]146
[0dd3a2f]147        if ( elsePart != 0 ) {
[50377a4]148                os << indent << "... else: " << endl;
149                os << indent+1;
150                elsePart->print( os, indent+1 );
[0dd3a2f]151        } // if
[51b7345]152}
153
[ba3706f]154SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ):
155        Statement(), condition( condition ), statements( statements ) {
[0dd3a2f]156}
[51b7345]157
[3be261a]158SwitchStmt::SwitchStmt( const SwitchStmt & other ):
159        Statement( other ), condition( maybeClone( other.condition ) ) {
[8688ce1]160        cloneAll( other.statements, statements );
[3be261a]161}
162
[50377a4]163void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]164        os << "Switch on condition: ";
[0dd3a2f]165        condition->print( os );
166        os << endl;
[51b7345]167
[50377a4]168        for ( const Statement * stmt : statements ) {
169                stmt->print( os, indent+1 );
170        }
[51b7345]171}
172
[a16764a6]173CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
[ba3706f]174        Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
[a16764a6]175        if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
[51b7345]176}
177
[3be261a]178CaseStmt::CaseStmt( const CaseStmt & other ) :
179        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
180        cloneAll( other.stmts, stmts );
181}
182
[ba3706f]183CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
184        CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
185        stmt->labels = labels;
186        return stmt;
[b2152e7a]187}
188
[50377a4]189void CaseStmt::print( std::ostream &os, Indenter indent ) const {
190        if ( isDefault() ) os << "Default ";
[0dd3a2f]191        else {
192                os << "Case ";
[50377a4]193                condition->print( os, indent );
[0dd3a2f]194        } // if
195        os << endl;
[51b7345]196
[50377a4]197        for ( Statement * stmt : stmts ) {
198                stmt->print( os, indent+1 );
199        }
[51b7345]200}
201
[ba3706f]202WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):
203        Statement(), condition( condition), body( body), isDoWhile( isDoWhile) {
[0dd3a2f]204}
[51b7345]205
[3be261a]206WhileStmt::WhileStmt( const WhileStmt & other ):
207        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
208}
209
[50377a4]210void WhileStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]211        os << "While on condition: " << endl ;
[50377a4]212        condition->print( os, indent+1 );
[51b7345]213
[50377a4]214        os << indent << "... with body: " << endl;
[51b7345]215
[50377a4]216        if ( body != 0 ) body->print( os, indent+1 );
[51b7345]217}
218
[ba3706f]219ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
220        Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
[0dd3a2f]221}
[51b7345]222
[3be261a]223ForStmt::ForStmt( const ForStmt & other ):
224        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
225                cloneAll( other.initialization, initialization );
226
227}
228
[50377a4]229void ForStmt::print( std::ostream &os, Indenter indent ) const {
230        Statement::print( os, indent ); // print labels
[be5aa1b]231
[50377a4]232        os << "For Statement" << endl;
[51b7345]233
[50377a4]234        if ( ! initialization.empty() ) {
235                os << indent << "... initialization: \n";
236                for ( Statement * stmt : initialization ) {
237                        os << indent+1;
238                        stmt->print( os, indent+1 );
239                }
[145f1fc]240        }
[51b7345]241
[50377a4]242        if ( condition != nullptr ) {
243                os << indent << "... condition: \n" << indent+1;
244                condition->print( os, indent+1 );
[bb8ea30]245        }
[51b7345]246
[50377a4]247        if ( increment != nullptr ) {
248                os << "\n" << indent << "... increment: \n" << indent+1;
249                increment->print( os, indent+1 );
[bb8ea30]250        }
[51b7345]251
[bb8ea30]252        if ( body != 0 ) {
[50377a4]253                os << "\n" << indent << "... with body: \n" << indent+1;
254                body->print( os, indent+1 );
[bb8ea30]255        }
[0dd3a2f]256        os << endl;
[51b7345]257}
258
[ba3706f]259ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
260                Statement(), kind(kind), expr(expr), target(target)     {
[daf1af8]261        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
262}
263
264ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
265        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
266}
267
[50377a4]268void ThrowStmt::print( std::ostream &os, Indenter indent) const {
269        if ( target ) os << "Non-Local ";
[daf1af8]270        os << "Throw Statement, raising: ";
[50377a4]271        expr->print(os, indent+1);
[daf1af8]272        if ( target ) {
[50377a4]273                os << "... at: ";
274                target->print(os, indent+1);
[daf1af8]275        }
276}
277
[ba3706f]278TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
279        Statement(), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
[0dd3a2f]280}
[51b7345]281
[3be261a]282TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
283        cloneAll( other.handlers, handlers );
[51b7345]284}
285
[50377a4]286void TryStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]287        os << "Try Statement" << endl;
[50377a4]288        os << indent << "... with block:" << endl << indent+1;
289        block->print( os, indent+1 );
[0dd3a2f]290
291        // handlers
[50377a4]292        os << indent << "... and handlers:" << endl;
293        for ( const CatchStmt * stmt : handlers ) {
294                os << indent+1;
295                stmt->print( os, indent+1 );
[406a6e6]296        }
[0dd3a2f]297
298        // finally block
299        if ( finallyBlock != 0 ) {
[50377a4]300                os << indent << "... and finally:" << endl << indent+1;
301                finallyBlock->print( os, indent+1 );
[0dd3a2f]302        } // if
[51b7345]303}
304
[ba3706f]305CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
306        Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
[50377a4]307                assertf( decl, "Catch clause must have a declaration." );
[0dd3a2f]308}
[51b7345]309
[3be261a]310CatchStmt::CatchStmt( const CatchStmt & other ) :
[ca78437]311        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
[3be261a]312}
313
[50377a4]314void CatchStmt::print( std::ostream &os, Indenter indent ) const {
[ca78437]315        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
[0dd3a2f]316
[50377a4]317        os << indent << "... catching: ";
318        decl->printShort( os, indent+1 );
319        os << endl;
[406a6e6]320
321        if ( cond ) {
[50377a4]322                os << indent << "... with conditional:" << endl << indent+1;
323                cond->print( os, indent+1 );
[406a6e6]324        }
325
[50377a4]326        os << indent << "... with block:" << endl;
327        os << indent+1;
328        body->print( os, indent+1 );
[51b7345]329}
330
331
[ba3706f]332FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
[51b7345]333}
334
[3be261a]335FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
336}
337
[50377a4]338void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
[f1b1e4c]339        os << "Finally Statement" << endl;
[50377a4]340        os << indent << "... with block:" << endl << indent+1;
341        block->print( os, indent+1 );
[51b7345]342}
343
[ba3706f]344WaitForStmt::WaitForStmt() : Statement() {
[135b431]345        timeout.time      = nullptr;
346        timeout.statement = nullptr;
347        timeout.condition = nullptr;
348        orelse .statement = nullptr;
349        orelse .condition = nullptr;
350}
351
352WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
353        clauses.reserve( other.clauses.size() );
354        for( auto & ocl : other.clauses ) {
355                clauses.emplace_back();
356                clauses.back().target.function = ocl.target.function->clone();
357                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
358                clauses.back().statement = ocl.statement->clone();
359                clauses.back().condition = ocl.condition->clone();
360        }
361
362        timeout.time      = other.timeout.time     ->clone();
363        timeout.statement = other.timeout.statement->clone();
364        timeout.condition = other.timeout.condition->clone();
365        orelse .statement = other.orelse .statement->clone();
366        orelse .condition = other.orelse .condition->clone();
367}
368
[50377a4]369void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
[135b431]370        os << "Waitfor Statement" << endl;
[50377a4]371        os << indent << "... with block:" << endl << indent+1;
[135b431]372        // block->print( os, indent + 4 );
373}
374
[61255ad]375
[3ca540f]376WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
[61255ad]377WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
378        cloneAll( other.exprs, exprs );
379}
380
381void WithStmt::print( std::ostream & os, Indenter indent ) const {
382        os << "With statement" << endl;
[44b4114]383        os << indent << "... with expressions: " << endl;
384        printAll( exprs, os, indent+1 );
[61255ad]385        os << indent << "... with statement:" << endl << indent+1;
386        stmt->print( os, indent+1 );
387}
388
389
[ba3706f]390NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
391}
[51b7345]392
[50377a4]393void NullStmt::print( std::ostream &os, Indenter ) const {
394        os << "Null Statement" << endl;
[51b7345]395}
396
[ba3706f]397ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
[f1b1e4c]398        assert( callStmt );
399}
400
[74d1804]401ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
[f1b1e4c]402}
403
[50377a4]404void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
[f1b1e4c]405        os << "Implicit Ctor Dtor Statement" << endl;
[50377a4]406        os << indent << "... with Ctor/Dtor: ";
407        callStmt->print( os, indent+1);
[f1b1e4c]408        os << endl;
409}
410
[0dd3a2f]411// Local Variables: //
412// tab-width: 4 //
413// mode: c++ //
414// compile-command: "make install" //
415// End: //
Note: See TracBrowser for help on using the repository browser.