source: src/SynTree/Statement.cc@ c6c6f2ae

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 c6c6f2ae was ea6332d, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Big header cleaning pass - commit 3

  • Property mode set to 100644
File size: 14.3 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
[daf1af8]11// Last Modified By : Andrew Beach
[406a6e6]12// Last Modified On : Mon Aug 14 12:26:00 2017
13// Update Count : 65
[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
[2871210]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
[2871210]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
[0dd3a2f]90BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
[2871210]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
[0dd3a2f]97BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
[2871210]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
[daf1af8]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
124IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
[2871210]125 Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
[51b73452]126
[3be261a]127IfStmt::IfStmt( const IfStmt & other ) :
128 Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}
129
[ac71a86]130IfStmt::~IfStmt() {
131 delete condition;
132 delete thenPart;
133 delete elsePart;
134}
[51b73452]135
[de62360d]136void IfStmt::print( std::ostream &os, int indent ) const {
[89231bc]137 os << "If on condition: " << endl ;
[60089f4]138 os << string( indent+4, ' ' );
[0dd3a2f]139 condition->print( os, indent + 4 );
[51b73452]140
[60089f4]141 os << string( indent+2, ' ' ) << "... then: " << endl;
[51b73452]142
[60089f4]143 os << string( indent+4, ' ' );
[0dd3a2f]144 thenPart->print( os, indent + 4 );
[51b73452]145
[0dd3a2f]146 if ( elsePart != 0 ) {
[60089f4]147 os << string( indent+2, ' ' ) << "... else: " << endl;
148 os << string( indent+4, ' ' );
[0dd3a2f]149 elsePart->print( os, indent + 4 );
150 } // if
[51b73452]151}
152
[8688ce1]153SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_statements ):
154 Statement( _labels ), condition( _condition ), statements( _statements ) {
[0dd3a2f]155}
[51b73452]156
[3be261a]157SwitchStmt::SwitchStmt( const SwitchStmt & other ):
158 Statement( other ), condition( maybeClone( other.condition ) ) {
[8688ce1]159 cloneAll( other.statements, statements );
[3be261a]160}
161
[51b73452]162SwitchStmt::~SwitchStmt() {
[0dd3a2f]163 delete condition;
[8688ce1]164 // destroy statements
[2037f82]165 deleteAll( statements );
[51b73452]166}
167
[de62360d]168void SwitchStmt::print( std::ostream &os, int indent ) const {
[89231bc]169 os << "Switch on condition: ";
[0dd3a2f]170 condition->print( os );
171 os << endl;
[51b73452]172
[8688ce1]173 // statements
[de62360d]174 std::list<Statement *>::const_iterator i;
[8688ce1]175 for ( i = statements.begin(); i != statements.end(); i++)
[de62360d]176 (*i)->print( os, indent + 4 );
[51b73452]177
[8688ce1]178 //for_each( statements.begin(), statements.end(), mem_fun( bind1st(&Statement::print ), os ));
[51b73452]179}
180
[3be261a]181CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
[2871210]182 Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
[0dd3a2f]183 if ( isDefault() && condition != 0 )
184 throw SemanticError("default with conditions");
[51b73452]185}
186
[3be261a]187CaseStmt::CaseStmt( const CaseStmt & other ) :
188 Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
189 cloneAll( other.stmts, stmts );
190}
191
[51b73452]192CaseStmt::~CaseStmt() {
[0dd3a2f]193 delete condition;
[2037f82]194 deleteAll( stmts );
[51b73452]195}
196
[8688ce1]197CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> stmts ) {
198 return new CaseStmt( labels, 0, stmts, true );
[b2152e7a]199}
200
[de62360d]201void CaseStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]202 os << string( indent, ' ' );
[51b73452]203
[de62360d]204 if ( isDefault() )
[0dd3a2f]205 os << "Default ";
206 else {
207 os << "Case ";
208 condition->print( os );
209 } // if
[51b73452]210
[0dd3a2f]211 os << endl;
[51b73452]212
[de62360d]213 std::list<Statement *>::const_iterator i;
[0dd3a2f]214 for ( i = stmts.begin(); i != stmts.end(); i++)
215 (*i )->print( os, indent + 4 );
[51b73452]216}
217
[0dd3a2f]218WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
219 Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
220}
[51b73452]221
[3be261a]222WhileStmt::WhileStmt( const WhileStmt & other ):
223 Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
224}
225
[a08ba92]226WhileStmt::~WhileStmt() {
[0dd3a2f]227 delete body;
[2037f82]228 delete condition;
[51b73452]229}
230
[de62360d]231void WhileStmt::print( std::ostream &os, int indent ) const {
[89231bc]232 os << "While on condition: " << endl ;
[0dd3a2f]233 condition->print( os, indent + 4 );
[51b73452]234
[44b5ca0]235 os << string( indent, ' ' ) << ".... with body: " << endl;
[51b73452]236
[0dd3a2f]237 if ( body != 0 ) body->print( os, indent + 4 );
[51b73452]238}
239
[145f1fc]240ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
[0dd3a2f]241 Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
242}
[51b73452]243
[3be261a]244ForStmt::ForStmt( const ForStmt & other ):
245 Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
246 cloneAll( other.initialization, initialization );
247
248}
249
[51b73452]250ForStmt::~ForStmt() {
[145f1fc]251 deleteAll( initialization );
[0dd3a2f]252 delete condition;
253 delete increment;
254 delete body;
[51b73452]255}
256
[de62360d]257void ForStmt::print( std::ostream &os, int indent ) const {
[89231bc]258 os << "Labels: {";
[de62360d]259 for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
[be5aa1b]260 os << *it << ",";
261 }
262 os << "}" << endl;
263
[44b5ca0]264 os << string( indent, ' ' ) << "For Statement" << endl ;
[51b73452]265
[3be261a]266 os << string( indent + 2, ' ' ) << "initialization: \n";
[145f1fc]267 for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
[bb8ea30]268 os << string( indent + 4, ' ' );
[145f1fc]269 (*it)->print( os, indent + 4 );
270 }
[51b73452]271
[3be261a]272 os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
[bb8ea30]273 if ( condition != 0 ) {
274 os << string( indent + 4, ' ' );
[0dd3a2f]275 condition->print( os, indent + 4 );
[bb8ea30]276 }
[51b73452]277
[3be261a]278 os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
[bb8ea30]279 if ( increment != 0 ) {
280 os << string( indent + 4, ' ' );
[0dd3a2f]281 increment->print( os, indent + 4 );
[bb8ea30]282 }
[51b73452]283
[3be261a]284 os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
[bb8ea30]285 if ( body != 0 ) {
286 os << string( indent + 4, ' ' );
[0dd3a2f]287 body->print( os, indent + 4 );
[bb8ea30]288 }
[51b73452]289
[0dd3a2f]290 os << endl;
[51b73452]291}
292
[daf1af8]293ThrowStmt::ThrowStmt( std::list<Label> labels, Kind kind, Expression * expr, Expression * target ) :
294 Statement( labels ), kind(kind), expr(expr), target(target) {
295 assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
296}
297
298ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
299 Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
300}
301
302ThrowStmt::~ThrowStmt() {
303 delete expr;
304 delete target;
305}
306
307void ThrowStmt::print( std::ostream &os, int indent) const {
308 if ( target ) {
309 os << "Non-Local ";
310 }
311 os << "Throw Statement, raising: ";
312 expr->print(os, indent + 4);
313 if ( target ) {
314 os << "At: ";
315 target->print(os, indent + 4);
316 }
317}
318
[046e04a]319TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<CatchStmt *> &_handlers, FinallyStmt *_finallyBlock ) :
[0dd3a2f]320 Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock ) {
321}
[51b73452]322
[3be261a]323TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
324 cloneAll( other.handlers, handlers );
[51b73452]325}
326
[a08ba92]327TryStmt::~TryStmt() {
[0dd3a2f]328 delete block;
[2037f82]329 deleteAll( handlers );
330 delete finallyBlock;
[51b73452]331}
332
[de62360d]333void TryStmt::print( std::ostream &os, int indent ) const {
[89231bc]334 os << "Try Statement" << endl;
[406a6e6]335 os << string( indent + 2, ' ' ) << "with block:" << endl;
336 os << string( indent + 4, ' ' );
[0dd3a2f]337 block->print( os, indent + 4 );
338
339 // handlers
[406a6e6]340 os << string( indent + 2, ' ' ) << "and handlers:" << endl;
341 for ( std::list<CatchStmt *>::const_iterator i = handlers.begin(); i != handlers.end(); i++) {
342 os << string( indent + 4, ' ' );
[0dd3a2f]343 (*i )->print( os, indent + 4 );
[406a6e6]344 }
[0dd3a2f]345
346 // finally block
347 if ( finallyBlock != 0 ) {
[406a6e6]348 os << string( indent + 2, ' ' ) << "and finally:" << endl;
[0dd3a2f]349 finallyBlock->print( os, indent + 4 );
350 } // if
[51b73452]351}
352
[ca78437]353CatchStmt::CatchStmt( std::list<Label> labels, Kind _kind, Declaration *_decl, Expression *_cond, Statement *_body ) :
354 Statement( labels ), kind ( _kind ), decl ( _decl ), cond ( _cond ), body( _body ) {
[0dd3a2f]355}
[51b73452]356
[3be261a]357CatchStmt::CatchStmt( const CatchStmt & other ) :
[ca78437]358 Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
[3be261a]359}
360
[a08ba92]361CatchStmt::~CatchStmt() {
[0dd3a2f]362 delete decl;
363 delete body;
[51b73452]364}
365
[de62360d]366void CatchStmt::print( std::ostream &os, int indent ) const {
[ca78437]367 os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
[0dd3a2f]368
[406a6e6]369 os << string( indent + 2, ' ' ) << "... catching: ";
[0dd3a2f]370 if ( decl ) {
371 decl->printShort( os, indent + 4 );
372 os << endl;
[ca78437]373 }
[0dd3a2f]374 else
[44b5ca0]375 os << string( indent + 4 , ' ' ) << ">>> Error: this catch clause must have a declaration <<<" << endl;
[406a6e6]376
377 if ( cond ) {
378 os << string( indent + 2, ' ' ) << "with conditional:" << endl;
379 os << string( indent + 4, ' ' );
380 cond->print( os, indent + 4 );
381 }
382 else
383 os << string( indent + 2, ' ' ) << "with no conditional" << endl;
384
385 os << string( indent + 2, ' ' ) << "with block:" << endl;
386 os << string( indent + 4, ' ' );
387 body->print( os, indent + 4 );
[51b73452]388}
389
390
[0dd3a2f]391FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
392 assert( labels.empty() ); // finally statement cannot be labeled
[51b73452]393}
394
[3be261a]395FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
396}
397
[0dd3a2f]398FinallyStmt::~FinallyStmt() {
399 delete block;
[51b73452]400}
401
[de62360d]402void FinallyStmt::print( std::ostream &os, int indent ) const {
[f1b1e4c]403 os << "Finally Statement" << endl;
[406a6e6]404 os << string( indent + 2, ' ' ) << "with block:" << endl;
405 os << string( indent + 4, ' ' );
[0dd3a2f]406 block->print( os, indent + 4 );
[51b73452]407}
408
409NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
410NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
411
[b3c36f4]412void NullStmt::print( std::ostream &os, __attribute__((unused)) int indent ) const {
[89231bc]413 os << "Null Statement" << endl ;
[51b73452]414}
415
[f1b1e4c]416ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
417 assert( callStmt );
418}
419
[74d1804]420ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
[f1b1e4c]421}
422
423ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
[74d1804]424 delete callStmt;
[f1b1e4c]425}
426
427void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
428 os << "Implicit Ctor Dtor Statement" << endl;
429 os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
430 callStmt->print( os, indent + 2);
431 os << endl;
432}
433
[3906301]434std::ostream & operator<<( std::ostream & out, const Statement * statement ) {
[8bf784a]435 if ( statement ) {
436 statement->print( out );
437 } else {
438 out << "nullptr";
439 }
[baf7fee]440 return out;
441}
442
[0dd3a2f]443// Local Variables: //
444// tab-width: 4 //
445// mode: c++ //
446// compile-command: "make install" //
447// End: //
Note: See TracBrowser for help on using the repository browser.