[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
|
---|
[6d49ea3] | 12 | // Last Modified On : Thu Aug 17 16:01:31 2017
|
---|
| 13 | // Update Count : 345
|
---|
[b87a5ed] | 14 | //
|
---|
| 15 |
|
---|
[d180746] | 16 | #include <cassert> // for assert, safe_dynamic_cast, assertf
|
---|
| 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 |
|
---|
[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() ) {
|
---|
[4f147cc] | 59 | StatementNode *node = safe_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 |
|
---|
[6d49ea3] | 101 | Expression * cond = ctl->condition ? maybeMoveBuild< Expression >(ctl->condition) : new VariableExpr( dynamic_cast<DeclarationWithType *>( dynamic_cast<DeclStmt *>( init.back() )->decl ) );
|
---|
| 102 | delete ctl;
|
---|
| 103 | return new IfStmt( noLabels, notZeroExpr( cond ), thenb, elseb, init );
|
---|
[2f22cc4] | 104 | }
|
---|
| 105 |
|
---|
| 106 | Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
|
---|
[7880579] | 107 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 108 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[20519b7] | 109 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
|
---|
[7ecbb7e] | 110 | return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
|
---|
[2f22cc4] | 111 | }
|
---|
[321f55d] | 112 | Statement *build_case( ExpressionNode *ctl ) {
|
---|
[7880579] | 113 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 114 | return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
|
---|
[321f55d] | 115 | }
|
---|
| 116 | Statement *build_default() {
|
---|
[7880579] | 117 | std::list< Statement * > branches;
|
---|
[321f55d] | 118 | return new CaseStmt( noLabels, nullptr, branches, true );
|
---|
| 119 | }
|
---|
[2f22cc4] | 120 |
|
---|
| 121 | Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
|
---|
[7880579] | 122 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 123 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[2f22cc4] | 124 | assert( branches.size() == 1 );
|
---|
[7ecbb7e] | 125 | return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
|
---|
[2f22cc4] | 126 | }
|
---|
| 127 |
|
---|
| 128 | Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
|
---|
[7880579] | 129 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 130 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[2f22cc4] | 131 | assert( branches.size() == 1 );
|
---|
| 132 |
|
---|
[7880579] | 133 | std::list< Statement * > init;
|
---|
[2f22cc4] | 134 | if ( forctl->init != 0 ) {
|
---|
[7ecbb7e] | 135 | buildMoveList( forctl->init, init );
|
---|
[2f22cc4] | 136 | } // if
|
---|
| 137 |
|
---|
| 138 | Expression *cond = 0;
|
---|
| 139 | if ( forctl->condition != 0 )
|
---|
[7ecbb7e] | 140 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
|
---|
[2f22cc4] | 141 |
|
---|
| 142 | Expression *incr = 0;
|
---|
| 143 | if ( forctl->change != 0 )
|
---|
[7ecbb7e] | 144 | incr = maybeMoveBuild< Expression >(forctl->change);
|
---|
[2f22cc4] | 145 |
|
---|
| 146 | delete forctl;
|
---|
| 147 | return new ForStmt( noLabels, init, cond, incr, branches.front() );
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[ab57786] | 150 | Statement *build_branch( BranchStmt::Type kind ) {
|
---|
| 151 | Statement * ret = new BranchStmt( noLabels, "", kind );
|
---|
| 152 | return ret;
|
---|
| 153 | }
|
---|
| 154 | Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
|
---|
| 155 | Statement * ret = new BranchStmt( noLabels, *identifier, kind );
|
---|
| 156 | delete identifier; // allocated by lexer
|
---|
| 157 | return ret;
|
---|
[321f55d] | 158 | }
|
---|
[8cc5cb0] | 159 | Statement *build_computedgoto( ExpressionNode *ctl ) {
|
---|
[7ecbb7e] | 160 | return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
|
---|
[8cc5cb0] | 161 | }
|
---|
| 162 |
|
---|
| 163 | Statement *build_return( ExpressionNode *ctl ) {
|
---|
[7880579] | 164 | std::list< Expression * > exps;
|
---|
[7ecbb7e] | 165 | buildMoveList( ctl, exps );
|
---|
[8cc5cb0] | 166 | return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
|
---|
| 167 | }
|
---|
[daf1af8] | 168 |
|
---|
[8cc5cb0] | 169 | Statement *build_throw( ExpressionNode *ctl ) {
|
---|
[7880579] | 170 | std::list< Expression * > exps;
|
---|
[7ecbb7e] | 171 | buildMoveList( ctl, exps );
|
---|
[ac71a86] | 172 | assertf( exps.size() < 2, "This means we are leaking memory");
|
---|
[daf1af8] | 173 | return new ThrowStmt( noLabels, ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | Statement *build_resume( ExpressionNode *ctl ) {
|
---|
| 177 | std::list< Expression * > exps;
|
---|
| 178 | buildMoveList( ctl, exps );
|
---|
| 179 | assertf( exps.size() < 2, "This means we are leaking memory");
|
---|
| 180 | return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
|
---|
[794c15b] | 184 | (void)ctl;
|
---|
| 185 | (void)target;
|
---|
| 186 | assertf( false, "resume at (non-local throw) is not yet supported," );
|
---|
[8cc5cb0] | 187 | }
|
---|
[321f55d] | 188 |
|
---|
[1d4580a] | 189 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
|
---|
[046e04a] | 190 | std::list< CatchStmt * > branches;
|
---|
| 191 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
|
---|
[4f147cc] | 192 | CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
|
---|
[7ecbb7e] | 193 | FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
|
---|
[1d4580a] | 194 | return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
|
---|
| 195 | }
|
---|
[ca78437] | 196 | Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
|
---|
[7880579] | 197 | std::list< Statement * > branches;
|
---|
[ca78437] | 198 | buildMoveList< Statement, StatementNode >( body, branches );
|
---|
[1d4580a] | 199 | assert( branches.size() == 1 );
|
---|
[ca78437] | 200 | return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
|
---|
[1d4580a] | 201 | }
|
---|
| 202 | Statement *build_finally( StatementNode *stmt ) {
|
---|
[7880579] | 203 | std::list< Statement * > branches;
|
---|
[7ecbb7e] | 204 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
[1d4580a] | 205 | assert( branches.size() == 1 );
|
---|
[7880579] | 206 | return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
|
---|
[1d4580a] | 207 | }
|
---|
| 208 |
|
---|
[135b431] | 209 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
|
---|
| 210 | auto node = new WaitForStmt();
|
---|
| 211 |
|
---|
| 212 | WaitForStmt::Target target;
|
---|
| 213 | target.function = maybeBuild<Expression>( targetExpr );
|
---|
| 214 | buildMoveList< Expression >( targetExpr, target.arguments );
|
---|
| 215 | delete targetExpr;
|
---|
| 216 |
|
---|
| 217 | node->clauses.push_back( WaitForStmt::Clause{
|
---|
| 218 | target,
|
---|
| 219 | maybeMoveBuild<Statement >( stmt ),
|
---|
| 220 | maybeMoveBuild<Expression>( when )
|
---|
| 221 | });
|
---|
| 222 |
|
---|
| 223 | return node;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
|
---|
| 227 | WaitForStmt::Target target;
|
---|
| 228 |
|
---|
| 229 | target.function = maybeBuild<Expression>( targetExpr );
|
---|
| 230 | buildMoveList< Expression >( targetExpr, target.arguments );
|
---|
| 231 | delete targetExpr;
|
---|
| 232 |
|
---|
| 233 | node->clauses.push_back( WaitForStmt::Clause{
|
---|
| 234 | std::move( target ),
|
---|
| 235 | maybeMoveBuild<Statement >( stmt ),
|
---|
| 236 | maybeMoveBuild<Expression>( when )
|
---|
| 237 | });
|
---|
| 238 |
|
---|
| 239 | return node;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
|
---|
| 243 | auto node = new WaitForStmt();
|
---|
| 244 |
|
---|
| 245 | if( timeout ) {
|
---|
| 246 | node->timeout.time = maybeMoveBuild<Expression>( timeout );
|
---|
| 247 | node->timeout.statement = maybeMoveBuild<Statement >( stmt );
|
---|
| 248 | node->timeout.condition = maybeMoveBuild<Expression>( when );
|
---|
| 249 | }
|
---|
| 250 | else {
|
---|
| 251 | node->orelse.statement = maybeMoveBuild<Statement >( stmt );
|
---|
| 252 | node->orelse.condition = maybeMoveBuild<Expression>( when );
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | return node;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) {
|
---|
| 259 | auto node = new WaitForStmt();
|
---|
| 260 |
|
---|
| 261 | node->timeout.time = maybeMoveBuild<Expression>( timeout );
|
---|
| 262 | node->timeout.statement = maybeMoveBuild<Statement >( stmt );
|
---|
| 263 | node->timeout.condition = maybeMoveBuild<Expression>( when );
|
---|
| 264 |
|
---|
| 265 | node->orelse.statement = maybeMoveBuild<Statement >( else_stmt );
|
---|
| 266 | node->orelse.condition = maybeMoveBuild<Expression>( else_when );
|
---|
| 267 |
|
---|
| 268 | return node;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | // WaitForStmt::Target build_waitfor( const std::string * name, ExpressionNode * arguments ) {
|
---|
| 272 | // return WaitForStmt::Clause{
|
---|
| 273 |
|
---|
| 274 | // };
|
---|
| 275 | // }
|
---|
| 276 |
|
---|
[e82aa9df] | 277 | Statement *build_compound( StatementNode *first ) {
|
---|
| 278 | CompoundStmt *cs = new CompoundStmt( noLabels );
|
---|
[7ecbb7e] | 279 | buildMoveList( first, cs->get_kids() );
|
---|
[b87a5ed] | 280 | return cs;
|
---|
[51b73452] | 281 | }
|
---|
| 282 |
|
---|
[e82aa9df] | 283 | Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
|
---|
[7f5566b] | 284 | std::list< Expression * > out, in;
|
---|
| 285 | std::list< ConstantExpr * > clob;
|
---|
[e82aa9df] | 286 |
|
---|
[7ecbb7e] | 287 | buildMoveList( output, out );
|
---|
| 288 | buildMoveList( input, in );
|
---|
| 289 | buildMoveList( clobber, clob );
|
---|
[e82aa9df] | 290 | return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
|
---|
[7f5566b] | 291 | }
|
---|
| 292 |
|
---|
[51b73452] | 293 | // Local Variables: //
|
---|
[b87a5ed] | 294 | // tab-width: 4 //
|
---|
| 295 | // mode: c++ //
|
---|
| 296 | // compile-command: "make install" //
|
---|
[51b73452] | 297 | // End: //
|
---|