[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 | // |
---|
| 7 | // Statement.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[7f5566b] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sat Jul 25 12:19:50 2015 |
---|
| 13 | // Update Count : 53 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <functional> |
---|
| 17 | #include <algorithm> |
---|
| 18 | #include <iostream> |
---|
| 19 | #include <list> |
---|
| 20 | #include <cassert> |
---|
| 21 | |
---|
| 22 | #include "Statement.h" |
---|
| 23 | #include "Expression.h" |
---|
| 24 | #include "Declaration.h" |
---|
| 25 | #include "Common/SemanticError.h" |
---|
| 26 | |
---|
| 27 | using std::string; |
---|
| 28 | using std::endl; |
---|
| 29 | |
---|
[2871210] | 30 | Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {} |
---|
[51b7345] | 31 | |
---|
[de62360d] | 32 | void Statement::print( std::ostream &, int indent ) const {} |
---|
[51b7345] | 33 | |
---|
| 34 | Statement::~Statement() {} |
---|
| 35 | |
---|
[2871210] | 36 | ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {} |
---|
[51b7345] | 37 | |
---|
| 38 | ExprStmt::~ExprStmt() {} |
---|
| 39 | |
---|
[de62360d] | 40 | void ExprStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 41 | os << string( indent, ' ' ) << "Expression Statement:" << endl; |
---|
[0dd3a2f] | 42 | expr->print( os, indent + 2 ); |
---|
[51b7345] | 43 | } |
---|
| 44 | |
---|
[7f5566b] | 45 | |
---|
| 46 | AsmStmt::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 ) {} |
---|
| 47 | |
---|
| 48 | AsmStmt::~AsmStmt() { |
---|
| 49 | delete instruction; |
---|
| 50 | deleteAll( output ); |
---|
| 51 | deleteAll( input ); |
---|
| 52 | deleteAll( clobber ); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void AsmStmt::print( std::ostream &os, int indent ) const { |
---|
| 56 | os << "Assembler Statement:" << endl; |
---|
| 57 | os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' ); |
---|
| 58 | instruction->print( os, indent + 2 ); |
---|
| 59 | if ( ! output.empty() ) { |
---|
| 60 | os << endl << std::string( indent, ' ' ) << "output: " << endl; |
---|
| 61 | printAll( output, os, indent + 2 ); |
---|
| 62 | } // if |
---|
| 63 | if ( ! input.empty() ) { |
---|
| 64 | os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' ); |
---|
| 65 | printAll( input, os, indent + 2 ); |
---|
| 66 | } // if |
---|
| 67 | if ( ! clobber.empty() ) { |
---|
| 68 | os << std::string( indent, ' ' ) << "clobber: " << endl; |
---|
| 69 | printAll( clobber, os, indent + 2 ); |
---|
| 70 | } // if |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
[51b7345] | 74 | const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" }; |
---|
| 75 | |
---|
[0dd3a2f] | 76 | BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) : |
---|
[2871210] | 77 | Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) { |
---|
[0dd3a2f] | 78 | //actually this is a syntactic error signaled by the parser |
---|
| 79 | if ( type == BranchStmt::Goto && target.size() == 0 ) |
---|
| 80 | throw SemanticError("goto without target"); |
---|
[51b7345] | 81 | } |
---|
| 82 | |
---|
[0dd3a2f] | 83 | BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) : |
---|
[2871210] | 84 | Statement( labels ), computedTarget( _computedTarget ), type( _type ) { |
---|
[0dd3a2f] | 85 | if ( type != BranchStmt::Goto || computedTarget == 0 ) |
---|
| 86 | throw SemanticError("Computed target not valid in branch statement"); |
---|
[51b7345] | 87 | } |
---|
| 88 | |
---|
[de62360d] | 89 | void BranchStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 90 | os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ; |
---|
[51b7345] | 91 | } |
---|
| 92 | |
---|
[0dd3a2f] | 93 | ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {} |
---|
[51b7345] | 94 | |
---|
| 95 | ReturnStmt::~ReturnStmt() { |
---|
[0dd3a2f] | 96 | delete expr; |
---|
[51b7345] | 97 | } |
---|
| 98 | |
---|
[de62360d] | 99 | void ReturnStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 100 | os << std::string( indent, ' ' ) << string ( isThrow? "Throw":"Return" ) << " Statement, returning: "; |
---|
[0dd3a2f] | 101 | if ( expr != 0 ) expr->print( os ); |
---|
| 102 | os << endl; |
---|
[51b7345] | 103 | } |
---|
| 104 | |
---|
| 105 | IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ): |
---|
[2871210] | 106 | Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {} |
---|
[51b7345] | 107 | |
---|
| 108 | IfStmt::~IfStmt() {} |
---|
| 109 | |
---|
[de62360d] | 110 | void IfStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 111 | os << string( indent, ' ' ) << "If on condition: " << endl ; |
---|
[0dd3a2f] | 112 | condition->print( os, indent + 4 ); |
---|
[51b7345] | 113 | |
---|
[44b5ca0] | 114 | os << string( indent, ' ' ) << ".... and branches: " << endl; |
---|
[51b7345] | 115 | |
---|
[0dd3a2f] | 116 | thenPart->print( os, indent + 4 ); |
---|
[51b7345] | 117 | |
---|
[0dd3a2f] | 118 | if ( elsePart != 0 ) { |
---|
| 119 | elsePart->print( os, indent + 4 ); |
---|
| 120 | } // if |
---|
[51b7345] | 121 | } |
---|
| 122 | |
---|
[0dd3a2f] | 123 | SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ): |
---|
[2871210] | 124 | Statement( _labels ), condition( _condition ), branches( _branches ) { |
---|
[0dd3a2f] | 125 | } |
---|
[51b7345] | 126 | |
---|
| 127 | SwitchStmt::~SwitchStmt() { |
---|
[0dd3a2f] | 128 | delete condition; |
---|
| 129 | // destroy branches |
---|
[51b7345] | 130 | } |
---|
| 131 | |
---|
[0dd3a2f] | 132 | void SwitchStmt::add_case( CaseStmt *c ) {} |
---|
[51b7345] | 133 | |
---|
[de62360d] | 134 | void SwitchStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 135 | os << string( indent, ' ' ) << "Switch on condition: "; |
---|
[0dd3a2f] | 136 | condition->print( os ); |
---|
| 137 | os << endl; |
---|
[51b7345] | 138 | |
---|
[0dd3a2f] | 139 | // branches |
---|
[de62360d] | 140 | std::list<Statement *>::const_iterator i; |
---|
[0dd3a2f] | 141 | for ( i = branches.begin(); i != branches.end(); i++) |
---|
[de62360d] | 142 | (*i)->print( os, indent + 4 ); |
---|
[51b7345] | 143 | |
---|
[0dd3a2f] | 144 | //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os )); |
---|
[51b7345] | 145 | } |
---|
| 146 | |
---|
[0dd3a2f] | 147 | CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) : |
---|
[2871210] | 148 | Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) { |
---|
[0dd3a2f] | 149 | if ( isDefault() && condition != 0 ) |
---|
| 150 | throw SemanticError("default with conditions"); |
---|
[51b7345] | 151 | } |
---|
| 152 | |
---|
| 153 | CaseStmt::~CaseStmt() { |
---|
[0dd3a2f] | 154 | delete condition; |
---|
[51b7345] | 155 | } |
---|
| 156 | |
---|
[b2152e7a] | 157 | CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) { |
---|
| 158 | return new CaseStmt( labels, 0, branches, true ); |
---|
| 159 | } |
---|
| 160 | |
---|
[de62360d] | 161 | void CaseStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 162 | os << string( indent, ' ' ); |
---|
[51b7345] | 163 | |
---|
[de62360d] | 164 | if ( isDefault() ) |
---|
[0dd3a2f] | 165 | os << "Default "; |
---|
| 166 | else { |
---|
| 167 | os << "Case "; |
---|
| 168 | condition->print( os ); |
---|
| 169 | } // if |
---|
[51b7345] | 170 | |
---|
[0dd3a2f] | 171 | os << endl; |
---|
[51b7345] | 172 | |
---|
[de62360d] | 173 | std::list<Statement *>::const_iterator i; |
---|
[0dd3a2f] | 174 | for ( i = stmts.begin(); i != stmts.end(); i++) |
---|
| 175 | (*i )->print( os, indent + 4 ); |
---|
[51b7345] | 176 | } |
---|
| 177 | |
---|
| 178 | //ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {} |
---|
[0dd3a2f] | 179 | ChooseStmt::ChooseStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ): |
---|
[2871210] | 180 | Statement( _labels ), condition( _condition ), branches( _branches ) { |
---|
[0dd3a2f] | 181 | } |
---|
[51b7345] | 182 | |
---|
| 183 | ChooseStmt::~ChooseStmt() { |
---|
[0dd3a2f] | 184 | delete condition; |
---|
[51b7345] | 185 | } |
---|
| 186 | |
---|
[0dd3a2f] | 187 | void ChooseStmt::add_case( CaseStmt *c ) {} |
---|
[51b7345] | 188 | |
---|
[de62360d] | 189 | void ChooseStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 190 | os << string( indent, ' ' ) << "Choose on condition: "; |
---|
[0dd3a2f] | 191 | condition->print( os ); |
---|
| 192 | os << endl; |
---|
[51b7345] | 193 | |
---|
[0dd3a2f] | 194 | // branches |
---|
[de62360d] | 195 | std::list<Statement *>::const_iterator i; |
---|
[0dd3a2f] | 196 | for ( i = branches.begin(); i != branches.end(); i++) |
---|
| 197 | (*i )->print( os, indent + 4 ); |
---|
[51b7345] | 198 | |
---|
[0dd3a2f] | 199 | //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os )); |
---|
[51b7345] | 200 | } |
---|
| 201 | |
---|
[de62360d] | 202 | void FallthruStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 203 | os << string( indent, ' ' ) << "Fall-through statement" << endl; |
---|
[51b7345] | 204 | } |
---|
| 205 | |
---|
[0dd3a2f] | 206 | WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ): |
---|
| 207 | Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) { |
---|
| 208 | } |
---|
[51b7345] | 209 | |
---|
[a08ba92] | 210 | WhileStmt::~WhileStmt() { |
---|
[0dd3a2f] | 211 | delete body; |
---|
[51b7345] | 212 | } |
---|
| 213 | |
---|
[de62360d] | 214 | void WhileStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 215 | os << string( indent, ' ' ) << "While on condition: " << endl ; |
---|
[0dd3a2f] | 216 | condition->print( os, indent + 4 ); |
---|
[51b7345] | 217 | |
---|
[44b5ca0] | 218 | os << string( indent, ' ' ) << ".... with body: " << endl; |
---|
[51b7345] | 219 | |
---|
[0dd3a2f] | 220 | if ( body != 0 ) body->print( os, indent + 4 ); |
---|
[51b7345] | 221 | } |
---|
| 222 | |
---|
[145f1fc] | 223 | ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ): |
---|
[0dd3a2f] | 224 | Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) { |
---|
| 225 | } |
---|
[51b7345] | 226 | |
---|
| 227 | ForStmt::~ForStmt() { |
---|
[145f1fc] | 228 | deleteAll( initialization ); |
---|
[0dd3a2f] | 229 | delete condition; |
---|
| 230 | delete increment; |
---|
| 231 | delete body; |
---|
[51b7345] | 232 | } |
---|
| 233 | |
---|
[de62360d] | 234 | void ForStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 235 | os << string( indent, ' ' ) << "Labels: {"; |
---|
[de62360d] | 236 | for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) { |
---|
[be5aa1b] | 237 | os << *it << ","; |
---|
| 238 | } |
---|
| 239 | os << "}" << endl; |
---|
| 240 | |
---|
[44b5ca0] | 241 | os << string( indent, ' ' ) << "For Statement" << endl ; |
---|
[51b7345] | 242 | |
---|
[44b5ca0] | 243 | os << string( indent + 2, ' ' ) << "initialization: \n"; |
---|
[145f1fc] | 244 | for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) { |
---|
| 245 | (*it)->print( os, indent + 4 ); |
---|
| 246 | } |
---|
[51b7345] | 247 | |
---|
[44b5ca0] | 248 | os << "\n" << string( indent + 2, ' ' ) << "condition: \n"; |
---|
[0dd3a2f] | 249 | if ( condition != 0 ) |
---|
| 250 | condition->print( os, indent + 4 ); |
---|
[51b7345] | 251 | |
---|
[44b5ca0] | 252 | os << "\n" << string( indent + 2, ' ' ) << "increment: \n"; |
---|
[0dd3a2f] | 253 | if ( increment != 0 ) |
---|
| 254 | increment->print( os, indent + 4 ); |
---|
[51b7345] | 255 | |
---|
[44b5ca0] | 256 | os << "\n" << string( indent + 2, ' ' ) << "statement block: \n"; |
---|
[0dd3a2f] | 257 | if ( body != 0 ) |
---|
| 258 | body->print( os, indent + 4 ); |
---|
[51b7345] | 259 | |
---|
[0dd3a2f] | 260 | os << endl; |
---|
[51b7345] | 261 | } |
---|
| 262 | |
---|
| 263 | TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) : |
---|
[0dd3a2f] | 264 | Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock ) { |
---|
| 265 | } |
---|
[51b7345] | 266 | |
---|
[0dd3a2f] | 267 | TryStmt::TryStmt( const TryStmt &other ) : Statement( other.labels ) { |
---|
| 268 | block = other.block; |
---|
| 269 | std::copy( other.handlers.begin(), other.handlers.end(), back_inserter( handlers ) ); |
---|
| 270 | finallyBlock = other.finallyBlock; |
---|
[51b7345] | 271 | } |
---|
| 272 | |
---|
[a08ba92] | 273 | TryStmt::~TryStmt() { |
---|
[0dd3a2f] | 274 | delete block; |
---|
[51b7345] | 275 | } |
---|
| 276 | |
---|
[de62360d] | 277 | void TryStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 278 | os << string( indent, ' ' ) << "Try Statement" << endl; |
---|
| 279 | os << string( indent + 2, ' ' ) << "with block: " << endl; |
---|
[0dd3a2f] | 280 | block->print( os, indent + 4 ); |
---|
| 281 | |
---|
| 282 | // handlers |
---|
[44b5ca0] | 283 | os << string( indent + 2, ' ' ) << "and handlers: " << endl; |
---|
[de62360d] | 284 | for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++) |
---|
[0dd3a2f] | 285 | (*i )->print( os, indent + 4 ); |
---|
| 286 | |
---|
| 287 | // finally block |
---|
| 288 | if ( finallyBlock != 0 ) { |
---|
[44b5ca0] | 289 | os << string( indent + 2, ' ' ) << "Finally block: " << endl; |
---|
[0dd3a2f] | 290 | finallyBlock->print( os, indent + 4 ); |
---|
| 291 | } // if |
---|
[51b7345] | 292 | } |
---|
| 293 | |
---|
| 294 | CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) : |
---|
[0dd3a2f] | 295 | Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) { |
---|
| 296 | } |
---|
[51b7345] | 297 | |
---|
[a08ba92] | 298 | CatchStmt::~CatchStmt() { |
---|
[0dd3a2f] | 299 | delete decl; |
---|
| 300 | delete body; |
---|
[51b7345] | 301 | } |
---|
| 302 | |
---|
[de62360d] | 303 | void CatchStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 304 | os << string( indent, ' ' ) << "Catch Statement" << endl; |
---|
[0dd3a2f] | 305 | |
---|
[44b5ca0] | 306 | os << string( indent, ' ' ) << "... catching" << endl; |
---|
[0dd3a2f] | 307 | if ( decl ) { |
---|
| 308 | decl->printShort( os, indent + 4 ); |
---|
| 309 | os << endl; |
---|
| 310 | } else if ( catchRest ) |
---|
[44b5ca0] | 311 | os << string( indent + 4 , ' ' ) << "the rest" << endl; |
---|
[0dd3a2f] | 312 | else |
---|
[44b5ca0] | 313 | os << string( indent + 4 , ' ' ) << ">>> Error: this catch clause must have a declaration <<<" << endl; |
---|
[51b7345] | 314 | } |
---|
| 315 | |
---|
| 316 | |
---|
[0dd3a2f] | 317 | FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) { |
---|
| 318 | assert( labels.empty() ); // finally statement cannot be labeled |
---|
[51b7345] | 319 | } |
---|
| 320 | |
---|
[0dd3a2f] | 321 | FinallyStmt::~FinallyStmt() { |
---|
| 322 | delete block; |
---|
[51b7345] | 323 | } |
---|
| 324 | |
---|
[de62360d] | 325 | void FinallyStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 326 | os << string( indent, ' ' ) << "Finally Statement" << endl; |
---|
| 327 | os << string( indent + 2, ' ' ) << "with block: " << endl; |
---|
[0dd3a2f] | 328 | block->print( os, indent + 4 ); |
---|
[51b7345] | 329 | } |
---|
| 330 | |
---|
| 331 | NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {} |
---|
| 332 | NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {} |
---|
| 333 | NullStmt::~NullStmt() {} |
---|
| 334 | |
---|
[de62360d] | 335 | void NullStmt::print( std::ostream &os, int indent ) const { |
---|
[44b5ca0] | 336 | os << string( indent, ' ' ) << "Null Statement" << endl ; |
---|
[51b7345] | 337 | } |
---|
| 338 | |
---|
[0dd3a2f] | 339 | // Local Variables: // |
---|
| 340 | // tab-width: 4 // |
---|
| 341 | // mode: c++ // |
---|
| 342 | // compile-command: "make install" // |
---|
| 343 | // End: // |
---|