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