[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
|
---|
[6d539f83] | 12 | // Last Modified On : Mon Apr 30 09:21:16 2018
|
---|
| 13 | // Update Count : 354
|
---|
[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
|
---|
[51b73452] | 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;
|
---|
[51b73452] | 31 |
|
---|
| 32 | using namespace std;
|
---|
| 33 |
|
---|
[b87a5ed] | 34 |
|
---|
[61fc4f6] | 35 | StatementNode::StatementNode( DeclarationNode * decl ) {
|
---|
[c0aa336] | 36 | assert( decl );
|
---|
[61fc4f6] | 37 | DeclarationNode * agg = decl->extractAggregate();
|
---|
[c0aa336] | 38 | if ( agg ) {
|
---|
[61fc4f6] | 39 | StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
|
---|
[c0aa336] | 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
|
---|
[ba3706f] | 52 | stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) );
|
---|
[c0aa336] | 53 | } // StatementNode::StatementNode
|
---|
[1d4580a] | 54 |
|
---|
[61fc4f6] | 55 | StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
|
---|
| 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() ) {
|
---|
[61fc4f6] | 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
|
---|
[61fc4f6] | 64 | StatementNode * node = dynamic_cast< StatementNode * >(prev);
|
---|
[7880579] | 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 |
|
---|
[61fc4f6] | 73 | Statement * build_expr( ExpressionNode * ctl ) {
|
---|
| 74 | Expression * e = maybeMoveBuild< Expression >( ctl );
|
---|
[8cc5cb0] | 75 |
|
---|
| 76 | if ( e )
|
---|
[ba3706f] | 77 | return new ExprStmt( e );
|
---|
[8cc5cb0] | 78 | else
|
---|
[ba3706f] | 79 | return new NullStmt();
|
---|
[8cc5cb0] | 80 | }
|
---|
| 81 |
|
---|
[ee3c93d] | 82 | Expression * build_if_control( IfCtl * ctl, std::list< Statement * > & init ) {
|
---|
[936e9f4] | 83 | if ( ctl->init != 0 ) {
|
---|
| 84 | buildMoveList( ctl->init, init );
|
---|
| 85 | } // if
|
---|
| 86 |
|
---|
[a01f7c94] | 87 | Expression * cond = nullptr;
|
---|
| 88 | if ( ctl->condition ) {
|
---|
| 89 | // compare the provided condition against 0
|
---|
[ee3c93d] | 90 | cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
|
---|
[a01f7c94] | 91 | } else {
|
---|
| 92 | for ( Statement * stmt : init ) {
|
---|
| 93 | // build the && of all of the declared variables compared against 0
|
---|
[e3e16bc] | 94 | DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
|
---|
| 95 | DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
|
---|
[a01f7c94] | 96 | Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
|
---|
| 97 | cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[6d49ea3] | 100 | delete ctl;
|
---|
[ee3c93d] | 101 | return cond;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
|
---|
| 105 | Statement * thenb, * elseb = 0;
|
---|
| 106 | std::list< Statement * > branches;
|
---|
| 107 | buildMoveList< Statement, StatementNode >( then_stmt, branches );
|
---|
| 108 | assert( branches.size() == 1 );
|
---|
| 109 | thenb = branches.front();
|
---|
| 110 |
|
---|
| 111 | if ( else_stmt ) {
|
---|
| 112 | std::list< Statement * > branches;
|
---|
| 113 | buildMoveList< Statement, StatementNode >( else_stmt, branches );
|
---|
| 114 | assert( branches.size() == 1 );
|
---|
| 115 | elseb = branches.front();
|
---|
| 116 | } // if
|
---|
| 117 |
|
---|
| 118 | std::list< Statement * > init;
|
---|
| 119 | Expression * cond = build_if_control( ctl, init );
|
---|
[ba3706f] | 120 | return new IfStmt( cond, thenb, elseb, init );
|
---|
[2f22cc4] | 121 | }
|
---|
| 122 |
|
---|
[61fc4f6] | 123 | Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
|
---|
[7880579] | 124 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 125 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[6a276a0] | 126 | if ( ! isSwitch ) { // choose statement
|
---|
| 127 | for ( Statement * stmt : branches ) {
|
---|
| 128 | CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
|
---|
| 129 | if ( ! caseStmt->stmts.empty() ) { // code after "case" => end of case list
|
---|
| 130 | CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
|
---|
| 131 | block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
|
---|
| 132 | } // if
|
---|
| 133 | } // for
|
---|
| 134 | } // if
|
---|
[20519b7] | 135 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
|
---|
[ba3706f] | 136 | return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
|
---|
[2f22cc4] | 137 | }
|
---|
[61fc4f6] | 138 | Statement * build_case( ExpressionNode * ctl ) {
|
---|
[7880579] | 139 | std::list< Statement * > branches;
|
---|
[ba3706f] | 140 | return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
|
---|
[321f55d] | 141 | }
|
---|
[61fc4f6] | 142 | Statement * build_default() {
|
---|
[7880579] | 143 | std::list< Statement * > branches;
|
---|
[ba3706f] | 144 | return new CaseStmt( nullptr, branches, true );
|
---|
[321f55d] | 145 | }
|
---|
[2f22cc4] | 146 |
|
---|
[61fc4f6] | 147 | Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind ) {
|
---|
[7880579] | 148 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 149 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[2f22cc4] | 150 | assert( branches.size() == 1 );
|
---|
[ee3c93d] | 151 |
|
---|
| 152 | std::list< Statement * > init;
|
---|
| 153 | return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, kind );
|
---|
[2f22cc4] | 154 | }
|
---|
| 155 |
|
---|
[61fc4f6] | 156 | Statement * build_for( ForCtl * forctl, StatementNode * stmt ) {
|
---|
[7880579] | 157 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 158 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[2f22cc4] | 159 | assert( branches.size() == 1 );
|
---|
| 160 |
|
---|
[7880579] | 161 | std::list< Statement * > init;
|
---|
[2f22cc4] | 162 | if ( forctl->init != 0 ) {
|
---|
[7ecbb7e] | 163 | buildMoveList( forctl->init, init );
|
---|
[2f22cc4] | 164 | } // if
|
---|
| 165 |
|
---|
[61fc4f6] | 166 | Expression * cond = 0;
|
---|
[2f22cc4] | 167 | if ( forctl->condition != 0 )
|
---|
[7ecbb7e] | 168 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
|
---|
[2f22cc4] | 169 |
|
---|
[61fc4f6] | 170 | Expression * incr = 0;
|
---|
[2f22cc4] | 171 | if ( forctl->change != 0 )
|
---|
[7ecbb7e] | 172 | incr = maybeMoveBuild< Expression >(forctl->change);
|
---|
[2f22cc4] | 173 |
|
---|
| 174 | delete forctl;
|
---|
[ba3706f] | 175 | return new ForStmt( init, cond, incr, branches.front() );
|
---|
[2f22cc4] | 176 | }
|
---|
| 177 |
|
---|
[61fc4f6] | 178 | Statement * build_branch( BranchStmt::Type kind ) {
|
---|
[ba3706f] | 179 | Statement * ret = new BranchStmt( "", kind );
|
---|
[ab57786] | 180 | return ret;
|
---|
| 181 | }
|
---|
[61fc4f6] | 182 | Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
|
---|
| 183 | Statement * ret = new BranchStmt( * identifier, kind );
|
---|
[ab57786] | 184 | delete identifier; // allocated by lexer
|
---|
| 185 | return ret;
|
---|
[321f55d] | 186 | }
|
---|
[61fc4f6] | 187 | Statement * build_computedgoto( ExpressionNode * ctl ) {
|
---|
[ba3706f] | 188 | return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
|
---|
[8cc5cb0] | 189 | }
|
---|
| 190 |
|
---|
[61fc4f6] | 191 | Statement * build_return( ExpressionNode * ctl ) {
|
---|
[7880579] | 192 | std::list< Expression * > exps;
|
---|
[7ecbb7e] | 193 | buildMoveList( ctl, exps );
|
---|
[ba3706f] | 194 | return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
|
---|
[8cc5cb0] | 195 | }
|
---|
[daf1af8] | 196 |
|
---|
[61fc4f6] | 197 | Statement * build_throw( ExpressionNode * ctl ) {
|
---|
[7880579] | 198 | std::list< Expression * > exps;
|
---|
[7ecbb7e] | 199 | buildMoveList( ctl, exps );
|
---|
[ac71a86] | 200 | assertf( exps.size() < 2, "This means we are leaking memory");
|
---|
[ba3706f] | 201 | return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
|
---|
[daf1af8] | 202 | }
|
---|
| 203 |
|
---|
[61fc4f6] | 204 | Statement * build_resume( ExpressionNode * ctl ) {
|
---|
[daf1af8] | 205 | std::list< Expression * > exps;
|
---|
| 206 | buildMoveList( ctl, exps );
|
---|
| 207 | assertf( exps.size() < 2, "This means we are leaking memory");
|
---|
[ba3706f] | 208 | return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
|
---|
[daf1af8] | 209 | }
|
---|
| 210 |
|
---|
[61fc4f6] | 211 | Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
|
---|
[794c15b] | 212 | (void)ctl;
|
---|
| 213 | (void)target;
|
---|
| 214 | assertf( false, "resume at (non-local throw) is not yet supported," );
|
---|
[8cc5cb0] | 215 | }
|
---|
[321f55d] | 216 |
|
---|
[61fc4f6] | 217 | Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
|
---|
[046e04a] | 218 | std::list< CatchStmt * > branches;
|
---|
| 219 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
|
---|
[61fc4f6] | 220 | CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
|
---|
| 221 | FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
|
---|
[ba3706f] | 222 | return new TryStmt( tryBlock, branches, finallyBlock );
|
---|
[1d4580a] | 223 | }
|
---|
[61fc4f6] | 224 | Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
|
---|
[7880579] | 225 | std::list< Statement * > branches;
|
---|
[ca78437] | 226 | buildMoveList< Statement, StatementNode >( body, branches );
|
---|
[1d4580a] | 227 | assert( branches.size() == 1 );
|
---|
[ba3706f] | 228 | return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
|
---|
[1d4580a] | 229 | }
|
---|
[61fc4f6] | 230 | Statement * build_finally( StatementNode * stmt ) {
|
---|
[7880579] | 231 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 232 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[1d4580a] | 233 | assert( branches.size() == 1 );
|
---|
[ba3706f] | 234 | return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
|
---|
[1d4580a] | 235 | }
|
---|
| 236 |
|
---|
[135b431] | 237 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
|
---|
| 238 | auto node = new WaitForStmt();
|
---|
| 239 |
|
---|
| 240 | WaitForStmt::Target target;
|
---|
| 241 | target.function = maybeBuild<Expression>( targetExpr );
|
---|
[2065609] | 242 |
|
---|
| 243 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
|
---|
| 244 | targetExpr->set_next( nullptr );
|
---|
| 245 | buildMoveList< Expression >( next, target.arguments );
|
---|
| 246 |
|
---|
[135b431] | 247 | delete targetExpr;
|
---|
| 248 |
|
---|
| 249 | node->clauses.push_back( WaitForStmt::Clause{
|
---|
| 250 | target,
|
---|
| 251 | maybeMoveBuild<Statement >( stmt ),
|
---|
[1dcd9554] | 252 | notZeroExpr( maybeMoveBuild<Expression>( when ) )
|
---|
[135b431] | 253 | });
|
---|
| 254 |
|
---|
| 255 | return node;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
|
---|
| 259 | WaitForStmt::Target target;
|
---|
| 260 | target.function = maybeBuild<Expression>( targetExpr );
|
---|
[2065609] | 261 |
|
---|
| 262 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
|
---|
| 263 | targetExpr->set_next( nullptr );
|
---|
| 264 | buildMoveList< Expression >( next, target.arguments );
|
---|
| 265 |
|
---|
[135b431] | 266 | delete targetExpr;
|
---|
| 267 |
|
---|
[310e5b7] | 268 | node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
|
---|
[135b431] | 269 | std::move( target ),
|
---|
| 270 | maybeMoveBuild<Statement >( stmt ),
|
---|
[1dcd9554] | 271 | notZeroExpr( maybeMoveBuild<Expression>( when ) )
|
---|
[135b431] | 272 | });
|
---|
| 273 |
|
---|
| 274 | return node;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
|
---|
| 278 | auto node = new WaitForStmt();
|
---|
| 279 |
|
---|
| 280 | if( timeout ) {
|
---|
| 281 | node->timeout.time = maybeMoveBuild<Expression>( timeout );
|
---|
| 282 | node->timeout.statement = maybeMoveBuild<Statement >( stmt );
|
---|
[1dcd9554] | 283 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
|
---|
[135b431] | 284 | }
|
---|
| 285 | else {
|
---|
[1dcd9554] | 286 | node->orelse.statement = maybeMoveBuild<Statement >( stmt );
|
---|
| 287 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
|
---|
[135b431] | 288 | }
|
---|
| 289 |
|
---|
| 290 | return node;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) {
|
---|
| 294 | auto node = new WaitForStmt();
|
---|
| 295 |
|
---|
| 296 | node->timeout.time = maybeMoveBuild<Expression>( timeout );
|
---|
| 297 | node->timeout.statement = maybeMoveBuild<Statement >( stmt );
|
---|
[1dcd9554] | 298 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
|
---|
[135b431] | 299 |
|
---|
[a378ca7] | 300 | node->orelse.statement = maybeMoveBuild<Statement >( else_stmt );
|
---|
[1dcd9554] | 301 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
|
---|
[135b431] | 302 |
|
---|
| 303 | return node;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[a378ca7] | 306 | WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
|
---|
| 307 | std::list< Expression * > e;
|
---|
| 308 | buildMoveList( exprs, e );
|
---|
| 309 | Statement * s = maybeMoveBuild<Statement>( stmt );
|
---|
| 310 | return new WithStmt( e, s );
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[61fc4f6] | 313 | Statement * build_compound( StatementNode * first ) {
|
---|
| 314 | CompoundStmt * cs = new CompoundStmt();
|
---|
[7ecbb7e] | 315 | buildMoveList( first, cs->get_kids() );
|
---|
[b87a5ed] | 316 | return cs;
|
---|
[51b73452] | 317 | }
|
---|
| 318 |
|
---|
[6d539f83] | 319 | Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
|
---|
[7f5566b] | 320 | std::list< Expression * > out, in;
|
---|
| 321 | std::list< ConstantExpr * > clob;
|
---|
[e82aa9df] | 322 |
|
---|
[7ecbb7e] | 323 | buildMoveList( output, out );
|
---|
| 324 | buildMoveList( input, in );
|
---|
| 325 | buildMoveList( clobber, clob );
|
---|
[ba3706f] | 326 | return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
|
---|
[7f5566b] | 327 | }
|
---|
| 328 |
|
---|
[6d539f83] | 329 | Statement * build_directive( string * directive ) {
|
---|
[cc32d83] | 330 | return new DirectiveStmt( *directive );
|
---|
[61fc4f6] | 331 | }
|
---|
| 332 |
|
---|
[51b73452] | 333 | // Local Variables: //
|
---|
[b87a5ed] | 334 | // tab-width: 4 //
|
---|
| 335 | // mode: c++ //
|
---|
| 336 | // compile-command: "make install" //
|
---|
[51b73452] | 337 | // End: //
|
---|