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