source: src/SynTree/Statement.cc@ 9bd6105

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 9bd6105 was 135b431, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

First step for waitfor statments, does nothing but is there

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