source: src/SynTree/Statement.cc @ 0ce063b

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0ce063b was ee3c93d, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Add support for while loops with control declarations

  • Property mode set to 100644
File size: 16.5 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
[6a276a0]36void Statement::print( std::ostream & os, Indenter indent ) const {
[50377a4]37        if ( ! labels.empty() ) {
[6a276a0]38                os << indent << "... Labels: {";
[50377a4]39                for ( const Label & l : labels ) {
40                        os << l << ",";
41                }
42                os << "}" << endl;
43        }
44}
[51b7345]45
46Statement::~Statement() {}
47
[ba3706f]48ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {}
[51b7345]49
[3be261a]50ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
51
52ExprStmt::~ExprStmt() {
53        delete expr;
54}
[51b7345]55
[50377a4]56void ExprStmt::print( std::ostream &os, Indenter indent ) const {
57        os << "Expression Statement:" << endl << indent+1;
58        expr->print( os, indent+1 );
[3be261a]59}
[51b7345]60
[7f5566b]61
[ba3706f]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 ) {}
[7f5566b]63
[3be261a]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
[7f5566b]70AsmStmt::~AsmStmt() {
71        delete instruction;
72        deleteAll( output );
73        deleteAll( input );
74        deleteAll( clobber );
75}
76
[50377a4]77void AsmStmt::print( std::ostream &os, Indenter indent ) const {
[7f5566b]78        os << "Assembler Statement:" << endl;
[50377a4]79        os << indent+1 << "instruction: " << endl << indent;
80        instruction->print( os, indent+1 );
[7f5566b]81        if ( ! output.empty() ) {
[50377a4]82                os << endl << indent+1 << "output: " << endl;
83                printAll( output, os, indent+1 );
[3be261a]84        } // if
[7f5566b]85        if ( ! input.empty() ) {
[50377a4]86                os << indent+1 << "input: " << endl;
87                printAll( input, os, indent+1 );
[7f5566b]88        } // if
89        if ( ! clobber.empty() ) {
[50377a4]90                os << indent+1 << "clobber: " << endl;
91                printAll( clobber, os, indent+1 );
[7f5566b]92        } // if
[3be261a]93}
[7f5566b]94
95
[cc32d83]96DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {}
97
98void DirectiveStmt::print( std::ostream &os, Indenter ) const {
99        os << "GCC Directive:" << directive << endl;
100}
101
102
[51b7345]103const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
104
[a16764a6]105BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
[ba3706f]106        Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
[0dd3a2f]107        //actually this is a syntactic error signaled by the parser
[b0dfbc4]108        if ( type == BranchStmt::Goto && target.empty() ) {
[a16764a6]109                SemanticError( target.get_statement()->location, "goto without target");
[b0dfbc4]110        }
[51b7345]111}
112
[a16764a6]113BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
[ba3706f]114        Statement(), computedTarget( computedTarget ), type( type ) {
[b0dfbc4]115        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
[a16764a6]116                SemanticError( computedTarget->location, "Computed target not valid in branch statement");
[b0dfbc4]117        }
[51b7345]118}
119
[50377a4]120void BranchStmt::print( std::ostream &os, Indenter indent ) const {
121        os << "Branch (" << brType[type] << ")" << endl ;
122        if ( target != "" ) os << indent+1 << "with target: " << target << endl;
123        if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
124        if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
[51b7345]125}
126
[ba3706f]127ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {}
[51b7345]128
[daf1af8]129ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
[3be261a]130
[51b7345]131ReturnStmt::~ReturnStmt() {
[0dd3a2f]132        delete expr;
[51b7345]133}
134
[50377a4]135void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
136        os << "Return Statement, returning: ";
137        if ( expr != nullptr ) {
138                os << endl << indent+1;
139                expr->print( os, indent+1 );
[89231bc]140        }
[0dd3a2f]141        os << endl;
[51b7345]142}
143
[ba3706f]144IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
145        Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
[51b7345]146
[3be261a]147IfStmt::IfStmt( const IfStmt & other ) :
[6d49ea3]148        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
149        cloneAll( other.initialization, initialization );
150}
[3be261a]151
[ac71a86]152IfStmt::~IfStmt() {
[6d49ea3]153        deleteAll( initialization );
[ac71a86]154        delete condition;
155        delete thenPart;
156        delete elsePart;
157}
[51b7345]158
[50377a4]159void IfStmt::print( std::ostream &os, Indenter indent ) const {
160        os << "If on condition: " << endl;
161        os << indent+1;
162        condition->print( os, indent+1 );
[51b7345]163
[6d49ea3]164        if ( !initialization.empty() ) {
[50377a4]165                os << indent << "... with initialization: \n";
166                for ( const Statement * stmt : initialization ) {
167                        os << indent+1;
168                        stmt->print( os, indent+1 );
[6d49ea3]169                }
170                os << endl;
171        }
172
[50377a4]173        os << indent << "... then: " << endl;
[51b7345]174
[50377a4]175        os << indent+1;
176        thenPart->print( os, indent+1 );
[51b7345]177
[0dd3a2f]178        if ( elsePart != 0 ) {
[50377a4]179                os << indent << "... else: " << endl;
180                os << indent+1;
181                elsePart->print( os, indent+1 );
[0dd3a2f]182        } // if
[51b7345]183}
184
[ba3706f]185SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ):
186        Statement(), condition( condition ), statements( statements ) {
[0dd3a2f]187}
[51b7345]188
[3be261a]189SwitchStmt::SwitchStmt( const SwitchStmt & other ):
190        Statement( other ), condition( maybeClone( other.condition ) ) {
[8688ce1]191        cloneAll( other.statements, statements );
[3be261a]192}
193
[51b7345]194SwitchStmt::~SwitchStmt() {
[0dd3a2f]195        delete condition;
[8688ce1]196        // destroy statements
[2037f82]197        deleteAll( statements );
[51b7345]198}
199
[50377a4]200void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]201        os << "Switch on condition: ";
[0dd3a2f]202        condition->print( os );
203        os << endl;
[51b7345]204
[50377a4]205        for ( const Statement * stmt : statements ) {
206                stmt->print( os, indent+1 );
207        }
[51b7345]208}
209
[a16764a6]210CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
[ba3706f]211        Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
[a16764a6]212        if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
[51b7345]213}
214
[3be261a]215CaseStmt::CaseStmt( const CaseStmt & other ) :
216        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
217        cloneAll( other.stmts, stmts );
218}
219
[51b7345]220CaseStmt::~CaseStmt() {
[0dd3a2f]221        delete condition;
[2037f82]222        deleteAll( stmts );
[51b7345]223}
224
[ba3706f]225CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
226        CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
227        stmt->labels = labels;
228        return stmt;
[b2152e7a]229}
230
[50377a4]231void CaseStmt::print( std::ostream &os, Indenter indent ) const {
[6a276a0]232        if ( isDefault() ) os << indent << "Default ";
[0dd3a2f]233        else {
[6a276a0]234                os << indent << "Case ";
[50377a4]235                condition->print( os, indent );
[0dd3a2f]236        } // if
237        os << endl;
[51b7345]238
[50377a4]239        for ( Statement * stmt : stmts ) {
[6a276a0]240                os << indent+1;
[50377a4]241                stmt->print( os, indent+1 );
242        }
[51b7345]243}
244
[ee3c93d]245WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ):
246        Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) {
[0dd3a2f]247}
[51b7345]248
[3be261a]249WhileStmt::WhileStmt( const WhileStmt & other ):
250        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
251}
252
[a08ba92]253WhileStmt::~WhileStmt() {
[0dd3a2f]254        delete body;
[2037f82]255        delete condition;
[51b7345]256}
257
[50377a4]258void WhileStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]259        os << "While on condition: " << endl ;
[50377a4]260        condition->print( os, indent+1 );
[51b7345]261
[50377a4]262        os << indent << "... with body: " << endl;
[51b7345]263
[50377a4]264        if ( body != 0 ) body->print( os, indent+1 );
[51b7345]265}
266
[ba3706f]267ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
268        Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
[0dd3a2f]269}
[51b7345]270
[3be261a]271ForStmt::ForStmt( const ForStmt & other ):
272        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
273                cloneAll( other.initialization, initialization );
274
275}
276
[51b7345]277ForStmt::~ForStmt() {
[145f1fc]278        deleteAll( initialization );
[0dd3a2f]279        delete condition;
280        delete increment;
281        delete body;
[51b7345]282}
283
[50377a4]284void ForStmt::print( std::ostream &os, Indenter indent ) const {
285        Statement::print( os, indent ); // print labels
[be5aa1b]286
[50377a4]287        os << "For Statement" << endl;
[51b7345]288
[50377a4]289        if ( ! initialization.empty() ) {
290                os << indent << "... initialization: \n";
291                for ( Statement * stmt : initialization ) {
292                        os << indent+1;
293                        stmt->print( os, indent+1 );
294                }
[145f1fc]295        }
[51b7345]296
[50377a4]297        if ( condition != nullptr ) {
298                os << indent << "... condition: \n" << indent+1;
299                condition->print( os, indent+1 );
[bb8ea30]300        }
[51b7345]301
[50377a4]302        if ( increment != nullptr ) {
303                os << "\n" << indent << "... increment: \n" << indent+1;
304                increment->print( os, indent+1 );
[bb8ea30]305        }
[51b7345]306
[bb8ea30]307        if ( body != 0 ) {
[50377a4]308                os << "\n" << indent << "... with body: \n" << indent+1;
309                body->print( os, indent+1 );
[bb8ea30]310        }
[0dd3a2f]311        os << endl;
[51b7345]312}
313
[ba3706f]314ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
315                Statement(), kind(kind), expr(expr), target(target)     {
[daf1af8]316        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
317}
318
319ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
320        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
321}
322
323ThrowStmt::~ThrowStmt() {
324        delete expr;
325        delete target;
326}
327
[50377a4]328void ThrowStmt::print( std::ostream &os, Indenter indent) const {
329        if ( target ) os << "Non-Local ";
[daf1af8]330        os << "Throw Statement, raising: ";
[50377a4]331        expr->print(os, indent+1);
[daf1af8]332        if ( target ) {
[50377a4]333                os << "... at: ";
334                target->print(os, indent+1);
[daf1af8]335        }
336}
337
[ba3706f]338TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
339        Statement(), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
[0dd3a2f]340}
[51b7345]341
[3be261a]342TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
343        cloneAll( other.handlers, handlers );
[51b7345]344}
345
[a08ba92]346TryStmt::~TryStmt() {
[0dd3a2f]347        delete block;
[2037f82]348        deleteAll( handlers );
349        delete finallyBlock;
[51b7345]350}
351
[50377a4]352void TryStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]353        os << "Try Statement" << endl;
[50377a4]354        os << indent << "... with block:" << endl << indent+1;
355        block->print( os, indent+1 );
[0dd3a2f]356
357        // handlers
[50377a4]358        os << indent << "... and handlers:" << endl;
359        for ( const CatchStmt * stmt : handlers ) {
360                os << indent+1;
361                stmt->print( os, indent+1 );
[406a6e6]362        }
[0dd3a2f]363
364        // finally block
365        if ( finallyBlock != 0 ) {
[50377a4]366                os << indent << "... and finally:" << endl << indent+1;
367                finallyBlock->print( os, indent+1 );
[0dd3a2f]368        } // if
[51b7345]369}
370
[ba3706f]371CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
372        Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
[50377a4]373                assertf( decl, "Catch clause must have a declaration." );
[0dd3a2f]374}
[51b7345]375
[3be261a]376CatchStmt::CatchStmt( const CatchStmt & other ) :
[ca78437]377        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
[3be261a]378}
379
[a08ba92]380CatchStmt::~CatchStmt() {
[0dd3a2f]381        delete decl;
382        delete body;
[51b7345]383}
384
[50377a4]385void CatchStmt::print( std::ostream &os, Indenter indent ) const {
[ca78437]386        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
[0dd3a2f]387
[50377a4]388        os << indent << "... catching: ";
389        decl->printShort( os, indent+1 );
390        os << endl;
[406a6e6]391
392        if ( cond ) {
[50377a4]393                os << indent << "... with conditional:" << endl << indent+1;
394                cond->print( os, indent+1 );
[406a6e6]395        }
396
[50377a4]397        os << indent << "... with block:" << endl;
398        os << indent+1;
399        body->print( os, indent+1 );
[51b7345]400}
401
402
[ba3706f]403FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
[51b7345]404}
405
[3be261a]406FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
407}
408
[0dd3a2f]409FinallyStmt::~FinallyStmt() {
410        delete block;
[51b7345]411}
412
[50377a4]413void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
[f1b1e4c]414        os << "Finally Statement" << endl;
[50377a4]415        os << indent << "... with block:" << endl << indent+1;
416        block->print( os, indent+1 );
[51b7345]417}
418
[ba3706f]419WaitForStmt::WaitForStmt() : Statement() {
[135b431]420        timeout.time      = nullptr;
421        timeout.statement = nullptr;
422        timeout.condition = nullptr;
423        orelse .statement = nullptr;
424        orelse .condition = nullptr;
425}
426
427WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
428        clauses.reserve( other.clauses.size() );
429        for( auto & ocl : other.clauses ) {
430                clauses.emplace_back();
431                clauses.back().target.function = ocl.target.function->clone();
432                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
433                clauses.back().statement = ocl.statement->clone();
434                clauses.back().condition = ocl.condition->clone();
435        }
436
437        timeout.time      = other.timeout.time     ->clone();
438        timeout.statement = other.timeout.statement->clone();
439        timeout.condition = other.timeout.condition->clone();
440        orelse .statement = other.orelse .statement->clone();
441        orelse .condition = other.orelse .condition->clone();
442}
443
444WaitForStmt::~WaitForStmt() {
445        for( auto & clause : clauses ) {
446                delete clause.target.function;
447                deleteAll( clause.target.arguments );
448                delete clause.statement;
449                delete clause.condition;
450        }
451
452        delete timeout.time;
453        delete timeout.statement;
454        delete timeout.condition;
455
456        delete orelse.statement;
457        delete orelse.condition;
458}
459
[50377a4]460void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
[135b431]461        os << "Waitfor Statement" << endl;
[b9f383f]462        indent += 1;
463        for( auto & clause : clauses ) {
464                os << indent << "target function :";
465                if(clause.target.function) { clause.target.function->print(os, indent + 1); }
466                os << endl << indent << "with arguments :" << endl;
467                for( auto & thing : clause.target.arguments) {
468                        if(thing) { thing->print(os, indent + 1); }
469                }
470                os << indent << " with statment :" << endl;
471                if(clause.statement) { clause.statement->print(os, indent + 1); }
472
473                os << indent << " with condition :" << endl;
474                if(clause.condition) { clause.condition->print(os, indent + 1); }
475        }
476
477        os << indent << " timeout of :" << endl;
478        if(timeout.time) { timeout.time->print(os, indent + 1); }
479
480        os << indent << " with statment :" << endl;
481        if(timeout.statement) { timeout.statement->print(os, indent + 1); }
482
483        os << indent << " with condition :" << endl;
484        if(timeout.condition) { timeout.condition->print(os, indent + 1); }
485
486
487        os << indent << " else :" << endl;
488        if(orelse.statement) { orelse.statement->print(os, indent + 1); }
489
490        os << indent << " with condition :" << endl;
491        if(orelse.condition) { orelse.condition->print(os, indent + 1); }
[135b431]492}
493
[61255ad]494
[3ca540f]495WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
[61255ad]496WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
497        cloneAll( other.exprs, exprs );
498}
499WithStmt::~WithStmt() {
500        deleteAll( exprs );
501        delete stmt;
502}
503
504void WithStmt::print( std::ostream & os, Indenter indent ) const {
505        os << "With statement" << endl;
[44b4114]506        os << indent << "... with expressions: " << endl;
507        printAll( exprs, os, indent+1 );
[61255ad]508        os << indent << "... with statement:" << endl << indent+1;
509        stmt->print( os, indent+1 );
510}
511
512
[ba3706f]513NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
514}
[51b7345]515
[6a276a0]516void NullStmt::print( std::ostream &os, Indenter indent ) const {
[50377a4]517        os << "Null Statement" << endl;
[6a276a0]518        Statement::print( os, indent );
[51b7345]519}
520
[ba3706f]521ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
[f1b1e4c]522        assert( callStmt );
523}
524
[74d1804]525ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
[f1b1e4c]526}
527
528ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
[74d1804]529        delete callStmt;
[f1b1e4c]530}
531
[50377a4]532void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
[f1b1e4c]533        os << "Implicit Ctor Dtor Statement" << endl;
[50377a4]534        os << indent << "... with Ctor/Dtor: ";
535        callStmt->print( os, indent+1);
[f1b1e4c]536        os << endl;
537}
538
[0dd3a2f]539// Local Variables: //
540// tab-width: 4 //
541// mode: c++ //
542// compile-command: "make install" //
543// End: //
Note: See TracBrowser for help on using the repository browser.