[b87a5ed] | 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 | // |
---|
[0f8e4ac] | 7 | // StatementNode.cc -- |
---|
[b87a5ed] | 8 | // |
---|
| 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 14:59:41 2015 |
---|
[a67b60e] | 11 | // Last Modified By : Peter A. Buhr |
---|
[e612146c] | 12 | // Last Modified On : Fri Sep 1 23:25:23 2017 |
---|
| 13 | // Update Count : 346 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[e3e16bc] | 16 | #include <cassert> // for assert, strict_dynamic_cast, assertf |
---|
[d180746] | 17 | #include <list> // for list |
---|
| 18 | #include <memory> // for unique_ptr |
---|
| 19 | #include <string> // for string |
---|
[51b7345] | 20 | |
---|
[d180746] | 21 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 22 | #include "Common/utility.h" // for maybeMoveBuild, maybeBuild |
---|
| 23 | #include "ParseNode.h" // for StatementNode, ExpressionNode, bui... |
---|
| 24 | #include "SynTree/Expression.h" // for Expression, ConstantExpr |
---|
| 25 | #include "SynTree/Label.h" // for Label, noLabels |
---|
[6d49ea3] | 26 | #include "SynTree/Declaration.h" |
---|
[d180746] | 27 | #include "SynTree/Statement.h" // for Statement, BranchStmt, CaseStmt |
---|
| 28 | #include "parserutility.h" // for notZeroExpr |
---|
| 29 | |
---|
| 30 | class Declaration; |
---|
[51b7345] | 31 | |
---|
| 32 | using namespace std; |
---|
| 33 | |
---|
[b87a5ed] | 34 | |
---|
[e82aa9df] | 35 | StatementNode::StatementNode( DeclarationNode *decl ) { |
---|
[c0aa336] | 36 | assert( decl ); |
---|
| 37 | DeclarationNode *agg = decl->extractAggregate(); |
---|
| 38 | if ( agg ) { |
---|
| 39 | StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) ); |
---|
| 40 | set_next( nextStmt ); |
---|
| 41 | if ( decl->get_next() ) { |
---|
| 42 | get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) ); |
---|
| 43 | decl->set_next( 0 ); |
---|
[1d4580a] | 44 | } // if |
---|
| 45 | } else { |
---|
[c0aa336] | 46 | if ( decl->get_next() ) { |
---|
| 47 | set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) ); |
---|
| 48 | decl->set_next( 0 ); |
---|
| 49 | } // if |
---|
| 50 | agg = decl; |
---|
[1d4580a] | 51 | } // if |
---|
[c0aa336] | 52 | stmt.reset( new DeclStmt( noLabels, maybeMoveBuild< Declaration >(agg) ) ); |
---|
| 53 | } // StatementNode::StatementNode |
---|
[1d4580a] | 54 | |
---|
[b87a5ed] | 55 | StatementNode *StatementNode::append_last_case( StatementNode *stmt ) { |
---|
[8cc5cb0] | 56 | StatementNode *prev = this; |
---|
[1d4580a] | 57 | // find end of list and maintain previous pointer |
---|
| 58 | for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) { |
---|
[e3e16bc] | 59 | StatementNode *node = strict_dynamic_cast< StatementNode * >(curr); |
---|
[ac71a86] | 60 | assert( dynamic_cast< CaseStmt * >(node->stmt.get()) ); |
---|
[8cc5cb0] | 61 | prev = curr; |
---|
| 62 | } // for |
---|
[e82aa9df] | 63 | // convert from StatementNode list to Statement list |
---|
[7880579] | 64 | StatementNode *node = dynamic_cast< StatementNode * >(prev); |
---|
| 65 | std::list< Statement * > stmts; |
---|
[7ecbb7e] | 66 | buildMoveList( stmt, stmts ); |
---|
[e82aa9df] | 67 | // splice any new Statements to end of current Statements |
---|
[ac71a86] | 68 | CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get()); |
---|
[8cc5cb0] | 69 | caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); |
---|
| 70 | return this; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | Statement *build_expr( ExpressionNode *ctl ) { |
---|
[7ecbb7e] | 74 | Expression *e = maybeMoveBuild< Expression >( ctl ); |
---|
[8cc5cb0] | 75 | |
---|
| 76 | if ( e ) |
---|
| 77 | return new ExprStmt( noLabels, e ); |
---|
| 78 | else |
---|
| 79 | return new NullStmt( noLabels ); |
---|
| 80 | } |
---|
| 81 | |
---|
[936e9f4] | 82 | Statement *build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) { |
---|
[2f22cc4] | 83 | Statement *thenb, *elseb = 0; |
---|
[7880579] | 84 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 85 | buildMoveList< Statement, StatementNode >( then_stmt, branches ); |
---|
[321f55d] | 86 | assert( branches.size() == 1 ); |
---|
[2f22cc4] | 87 | thenb = branches.front(); |
---|
| 88 | |
---|
| 89 | if ( else_stmt ) { |
---|
[7880579] | 90 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 91 | buildMoveList< Statement, StatementNode >( else_stmt, branches ); |
---|
[321f55d] | 92 | assert( branches.size() == 1 ); |
---|
[2f22cc4] | 93 | elseb = branches.front(); |
---|
| 94 | } // if |
---|
[135b431] | 95 | |
---|
[936e9f4] | 96 | std::list< Statement * > init; |
---|
| 97 | if ( ctl->init != 0 ) { |
---|
| 98 | buildMoveList( ctl->init, init ); |
---|
| 99 | } // if |
---|
| 100 | |
---|
[a01f7c94] | 101 | Expression * cond = nullptr; |
---|
| 102 | if ( ctl->condition ) { |
---|
| 103 | // compare the provided condition against 0 |
---|
| 104 | cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) ); |
---|
| 105 | } else { |
---|
| 106 | for ( Statement * stmt : init ) { |
---|
| 107 | // build the && of all of the declared variables compared against 0 |
---|
[e3e16bc] | 108 | DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt ); |
---|
| 109 | DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl ); |
---|
[a01f7c94] | 110 | Expression * nze = notZeroExpr( new VariableExpr( dwt ) ); |
---|
| 111 | cond = cond ? new LogicalExpr( cond, nze, true ) : nze; |
---|
| 112 | } |
---|
| 113 | } |
---|
[6d49ea3] | 114 | delete ctl; |
---|
[a01f7c94] | 115 | return new IfStmt( noLabels, cond, thenb, elseb, init ); |
---|
[2f22cc4] | 116 | } |
---|
| 117 | |
---|
| 118 | Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) { |
---|
[7880579] | 119 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 120 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[20519b7] | 121 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements |
---|
[7ecbb7e] | 122 | return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches ); |
---|
[2f22cc4] | 123 | } |
---|
[321f55d] | 124 | Statement *build_case( ExpressionNode *ctl ) { |
---|
[7880579] | 125 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 126 | return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches ); |
---|
[321f55d] | 127 | } |
---|
| 128 | Statement *build_default() { |
---|
[7880579] | 129 | std::list< Statement * > branches; |
---|
[321f55d] | 130 | return new CaseStmt( noLabels, nullptr, branches, true ); |
---|
| 131 | } |
---|
[2f22cc4] | 132 | |
---|
| 133 | Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) { |
---|
[7880579] | 134 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 135 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[2f22cc4] | 136 | assert( branches.size() == 1 ); |
---|
[7ecbb7e] | 137 | return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind ); |
---|
[2f22cc4] | 138 | } |
---|
| 139 | |
---|
| 140 | Statement *build_for( ForCtl *forctl, StatementNode *stmt ) { |
---|
[7880579] | 141 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 142 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[2f22cc4] | 143 | assert( branches.size() == 1 ); |
---|
| 144 | |
---|
[7880579] | 145 | std::list< Statement * > init; |
---|
[2f22cc4] | 146 | if ( forctl->init != 0 ) { |
---|
[7ecbb7e] | 147 | buildMoveList( forctl->init, init ); |
---|
[2f22cc4] | 148 | } // if |
---|
| 149 | |
---|
| 150 | Expression *cond = 0; |
---|
| 151 | if ( forctl->condition != 0 ) |
---|
[7ecbb7e] | 152 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) ); |
---|
[2f22cc4] | 153 | |
---|
| 154 | Expression *incr = 0; |
---|
| 155 | if ( forctl->change != 0 ) |
---|
[7ecbb7e] | 156 | incr = maybeMoveBuild< Expression >(forctl->change); |
---|
[2f22cc4] | 157 | |
---|
| 158 | delete forctl; |
---|
| 159 | return new ForStmt( noLabels, init, cond, incr, branches.front() ); |
---|
| 160 | } |
---|
| 161 | |
---|
[ab57786] | 162 | Statement *build_branch( BranchStmt::Type kind ) { |
---|
| 163 | Statement * ret = new BranchStmt( noLabels, "", kind ); |
---|
| 164 | return ret; |
---|
| 165 | } |
---|
| 166 | Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) { |
---|
| 167 | Statement * ret = new BranchStmt( noLabels, *identifier, kind ); |
---|
| 168 | delete identifier; // allocated by lexer |
---|
| 169 | return ret; |
---|
[321f55d] | 170 | } |
---|
[8cc5cb0] | 171 | Statement *build_computedgoto( ExpressionNode *ctl ) { |
---|
[7ecbb7e] | 172 | return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto ); |
---|
[8cc5cb0] | 173 | } |
---|
| 174 | |
---|
| 175 | Statement *build_return( ExpressionNode *ctl ) { |
---|
[7880579] | 176 | std::list< Expression * > exps; |
---|
[7ecbb7e] | 177 | buildMoveList( ctl, exps ); |
---|
[8cc5cb0] | 178 | return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr ); |
---|
| 179 | } |
---|
[daf1af8] | 180 | |
---|
[8cc5cb0] | 181 | Statement *build_throw( ExpressionNode *ctl ) { |
---|
[7880579] | 182 | std::list< Expression * > exps; |
---|
[7ecbb7e] | 183 | buildMoveList( ctl, exps ); |
---|
[ac71a86] | 184 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
[daf1af8] | 185 | return new ThrowStmt( noLabels, ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr ); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | Statement *build_resume( ExpressionNode *ctl ) { |
---|
| 189 | std::list< Expression * > exps; |
---|
| 190 | buildMoveList( ctl, exps ); |
---|
| 191 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
| 192 | return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr ); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) { |
---|
[794c15b] | 196 | (void)ctl; |
---|
| 197 | (void)target; |
---|
| 198 | assertf( false, "resume at (non-local throw) is not yet supported," ); |
---|
[8cc5cb0] | 199 | } |
---|
[321f55d] | 200 | |
---|
[1d4580a] | 201 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) { |
---|
[046e04a] | 202 | std::list< CatchStmt * > branches; |
---|
| 203 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches ); |
---|
[e3e16bc] | 204 | CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt)); |
---|
[7ecbb7e] | 205 | FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) ); |
---|
[1d4580a] | 206 | return new TryStmt( noLabels, tryBlock, branches, finallyBlock ); |
---|
| 207 | } |
---|
[ca78437] | 208 | Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) { |
---|
[7880579] | 209 | std::list< Statement * > branches; |
---|
[ca78437] | 210 | buildMoveList< Statement, StatementNode >( body, branches ); |
---|
[1d4580a] | 211 | assert( branches.size() == 1 ); |
---|
[ca78437] | 212 | return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() ); |
---|
[1d4580a] | 213 | } |
---|
| 214 | Statement *build_finally( StatementNode *stmt ) { |
---|
[7880579] | 215 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 216 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[1d4580a] | 217 | assert( branches.size() == 1 ); |
---|
[7880579] | 218 | return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) ); |
---|
[1d4580a] | 219 | } |
---|
| 220 | |
---|
[135b431] | 221 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) { |
---|
| 222 | auto node = new WaitForStmt(); |
---|
| 223 | |
---|
| 224 | WaitForStmt::Target target; |
---|
| 225 | target.function = maybeBuild<Expression>( targetExpr ); |
---|
[2065609] | 226 | |
---|
| 227 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() ); |
---|
| 228 | targetExpr->set_next( nullptr ); |
---|
| 229 | buildMoveList< Expression >( next, target.arguments ); |
---|
| 230 | |
---|
[135b431] | 231 | delete targetExpr; |
---|
| 232 | |
---|
| 233 | node->clauses.push_back( WaitForStmt::Clause{ |
---|
| 234 | target, |
---|
| 235 | maybeMoveBuild<Statement >( stmt ), |
---|
[1dcd9554] | 236 | notZeroExpr( maybeMoveBuild<Expression>( when ) ) |
---|
[135b431] | 237 | }); |
---|
| 238 | |
---|
| 239 | return node; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) { |
---|
| 243 | WaitForStmt::Target target; |
---|
| 244 | target.function = maybeBuild<Expression>( targetExpr ); |
---|
[2065609] | 245 | |
---|
| 246 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() ); |
---|
| 247 | targetExpr->set_next( nullptr ); |
---|
| 248 | buildMoveList< Expression >( next, target.arguments ); |
---|
| 249 | |
---|
[135b431] | 250 | delete targetExpr; |
---|
| 251 | |
---|
| 252 | node->clauses.push_back( WaitForStmt::Clause{ |
---|
| 253 | std::move( target ), |
---|
| 254 | maybeMoveBuild<Statement >( stmt ), |
---|
[1dcd9554] | 255 | notZeroExpr( maybeMoveBuild<Expression>( when ) ) |
---|
[135b431] | 256 | }); |
---|
| 257 | |
---|
| 258 | return node; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) { |
---|
| 262 | auto node = new WaitForStmt(); |
---|
| 263 | |
---|
| 264 | if( timeout ) { |
---|
| 265 | node->timeout.time = maybeMoveBuild<Expression>( timeout ); |
---|
| 266 | node->timeout.statement = maybeMoveBuild<Statement >( stmt ); |
---|
[1dcd9554] | 267 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); |
---|
[135b431] | 268 | } |
---|
| 269 | else { |
---|
[1dcd9554] | 270 | node->orelse.statement = maybeMoveBuild<Statement >( stmt ); |
---|
| 271 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); |
---|
[135b431] | 272 | } |
---|
| 273 | |
---|
| 274 | return node; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) { |
---|
| 278 | auto node = new WaitForStmt(); |
---|
| 279 | |
---|
| 280 | node->timeout.time = maybeMoveBuild<Expression>( timeout ); |
---|
| 281 | node->timeout.statement = maybeMoveBuild<Statement >( stmt ); |
---|
[1dcd9554] | 282 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); |
---|
[135b431] | 283 | |
---|
| 284 | node->orelse.statement = maybeMoveBuild<Statement >( else_stmt ); |
---|
[1dcd9554] | 285 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) ); |
---|
[135b431] | 286 | |
---|
| 287 | return node; |
---|
| 288 | } |
---|
| 289 | |
---|
[e82aa9df] | 290 | Statement *build_compound( StatementNode *first ) { |
---|
| 291 | CompoundStmt *cs = new CompoundStmt( noLabels ); |
---|
[7ecbb7e] | 292 | buildMoveList( first, cs->get_kids() ); |
---|
[b87a5ed] | 293 | return cs; |
---|
[51b7345] | 294 | } |
---|
| 295 | |
---|
[e612146c] | 296 | Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) { |
---|
[7f5566b] | 297 | std::list< Expression * > out, in; |
---|
| 298 | std::list< ConstantExpr * > clob; |
---|
[e82aa9df] | 299 | |
---|
[7ecbb7e] | 300 | buildMoveList( output, out ); |
---|
| 301 | buildMoveList( input, in ); |
---|
| 302 | buildMoveList( clobber, clob ); |
---|
[e82aa9df] | 303 | return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); |
---|
[7f5566b] | 304 | } |
---|
| 305 | |
---|
[51b7345] | 306 | // Local Variables: // |
---|
[b87a5ed] | 307 | // tab-width: 4 // |
---|
| 308 | // mode: c++ // |
---|
| 309 | // compile-command: "make install" // |
---|
[51b7345] | 310 | // End: // |
---|