[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 |
---|
[20519b7] | 12 | // Last Modified On : Tue Jul 11 21:23:15 2017 |
---|
| 13 | // Update Count : 331 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <list> |
---|
| 17 | #include <algorithm> |
---|
| 18 | #include <cassert> |
---|
| 19 | |
---|
| 20 | #include "ParseNode.h" |
---|
| 21 | #include "SynTree/Statement.h" |
---|
| 22 | #include "SynTree/Expression.h" |
---|
[a67b60e] | 23 | #include "parserutility.h" |
---|
[d3b7937] | 24 | #include "Common/utility.h" |
---|
[51b7345] | 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
[b87a5ed] | 28 | |
---|
[e82aa9df] | 29 | StatementNode::StatementNode( DeclarationNode *decl ) { |
---|
[c0aa336] | 30 | assert( decl ); |
---|
| 31 | DeclarationNode *agg = decl->extractAggregate(); |
---|
| 32 | if ( agg ) { |
---|
| 33 | StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) ); |
---|
| 34 | set_next( nextStmt ); |
---|
| 35 | if ( decl->get_next() ) { |
---|
| 36 | get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) ); |
---|
| 37 | decl->set_next( 0 ); |
---|
[1d4580a] | 38 | } // if |
---|
| 39 | } else { |
---|
[c0aa336] | 40 | if ( decl->get_next() ) { |
---|
| 41 | set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) ); |
---|
| 42 | decl->set_next( 0 ); |
---|
| 43 | } // if |
---|
| 44 | agg = decl; |
---|
[1d4580a] | 45 | } // if |
---|
[c0aa336] | 46 | stmt.reset( new DeclStmt( noLabels, maybeMoveBuild< Declaration >(agg) ) ); |
---|
| 47 | } // StatementNode::StatementNode |
---|
[1d4580a] | 48 | |
---|
[b87a5ed] | 49 | StatementNode *StatementNode::append_last_case( StatementNode *stmt ) { |
---|
[8cc5cb0] | 50 | StatementNode *prev = this; |
---|
[1d4580a] | 51 | // find end of list and maintain previous pointer |
---|
| 52 | for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) { |
---|
[4f147cc] | 53 | StatementNode *node = safe_dynamic_cast< StatementNode * >(curr); |
---|
[ac71a86] | 54 | assert( dynamic_cast< CaseStmt * >(node->stmt.get()) ); |
---|
[8cc5cb0] | 55 | prev = curr; |
---|
| 56 | } // for |
---|
[e82aa9df] | 57 | // convert from StatementNode list to Statement list |
---|
[7880579] | 58 | StatementNode *node = dynamic_cast< StatementNode * >(prev); |
---|
| 59 | std::list< Statement * > stmts; |
---|
[7ecbb7e] | 60 | buildMoveList( stmt, stmts ); |
---|
[e82aa9df] | 61 | // splice any new Statements to end of current Statements |
---|
[ac71a86] | 62 | CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get()); |
---|
[8cc5cb0] | 63 | caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); |
---|
| 64 | return this; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | Statement *build_expr( ExpressionNode *ctl ) { |
---|
[7ecbb7e] | 68 | Expression *e = maybeMoveBuild< Expression >( ctl ); |
---|
[8cc5cb0] | 69 | |
---|
| 70 | if ( e ) |
---|
| 71 | return new ExprStmt( noLabels, e ); |
---|
| 72 | else |
---|
| 73 | return new NullStmt( noLabels ); |
---|
| 74 | } |
---|
| 75 | |
---|
[2f22cc4] | 76 | Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) { |
---|
| 77 | Statement *thenb, *elseb = 0; |
---|
[7880579] | 78 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 79 | buildMoveList< Statement, StatementNode >( then_stmt, branches ); |
---|
[321f55d] | 80 | assert( branches.size() == 1 ); |
---|
[2f22cc4] | 81 | thenb = branches.front(); |
---|
| 82 | |
---|
| 83 | if ( else_stmt ) { |
---|
[7880579] | 84 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 85 | buildMoveList< Statement, StatementNode >( else_stmt, branches ); |
---|
[321f55d] | 86 | assert( branches.size() == 1 ); |
---|
[2f22cc4] | 87 | elseb = branches.front(); |
---|
| 88 | } // if |
---|
[7ecbb7e] | 89 | return new IfStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), thenb, elseb ); |
---|
[2f22cc4] | 90 | } |
---|
| 91 | |
---|
| 92 | Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) { |
---|
[7880579] | 93 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 94 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[20519b7] | 95 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements |
---|
[7ecbb7e] | 96 | return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches ); |
---|
[2f22cc4] | 97 | } |
---|
[321f55d] | 98 | Statement *build_case( ExpressionNode *ctl ) { |
---|
[7880579] | 99 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 100 | return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches ); |
---|
[321f55d] | 101 | } |
---|
| 102 | Statement *build_default() { |
---|
[7880579] | 103 | std::list< Statement * > branches; |
---|
[321f55d] | 104 | return new CaseStmt( noLabels, nullptr, branches, true ); |
---|
| 105 | } |
---|
[2f22cc4] | 106 | |
---|
| 107 | Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) { |
---|
[7880579] | 108 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 109 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[2f22cc4] | 110 | assert( branches.size() == 1 ); |
---|
[7ecbb7e] | 111 | return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind ); |
---|
[2f22cc4] | 112 | } |
---|
| 113 | |
---|
| 114 | Statement *build_for( ForCtl *forctl, StatementNode *stmt ) { |
---|
[7880579] | 115 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 116 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[2f22cc4] | 117 | assert( branches.size() == 1 ); |
---|
| 118 | |
---|
[7880579] | 119 | std::list< Statement * > init; |
---|
[2f22cc4] | 120 | if ( forctl->init != 0 ) { |
---|
[7ecbb7e] | 121 | buildMoveList( forctl->init, init ); |
---|
[2f22cc4] | 122 | } // if |
---|
| 123 | |
---|
| 124 | Expression *cond = 0; |
---|
| 125 | if ( forctl->condition != 0 ) |
---|
[7ecbb7e] | 126 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) ); |
---|
[2f22cc4] | 127 | |
---|
| 128 | Expression *incr = 0; |
---|
| 129 | if ( forctl->change != 0 ) |
---|
[7ecbb7e] | 130 | incr = maybeMoveBuild< Expression >(forctl->change); |
---|
[2f22cc4] | 131 | |
---|
| 132 | delete forctl; |
---|
| 133 | return new ForStmt( noLabels, init, cond, incr, branches.front() ); |
---|
| 134 | } |
---|
| 135 | |
---|
[ab57786] | 136 | Statement *build_branch( BranchStmt::Type kind ) { |
---|
| 137 | Statement * ret = new BranchStmt( noLabels, "", kind ); |
---|
| 138 | return ret; |
---|
| 139 | } |
---|
| 140 | Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) { |
---|
| 141 | Statement * ret = new BranchStmt( noLabels, *identifier, kind ); |
---|
| 142 | delete identifier; // allocated by lexer |
---|
| 143 | return ret; |
---|
[321f55d] | 144 | } |
---|
[8cc5cb0] | 145 | Statement *build_computedgoto( ExpressionNode *ctl ) { |
---|
[7ecbb7e] | 146 | return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto ); |
---|
[8cc5cb0] | 147 | } |
---|
| 148 | |
---|
| 149 | Statement *build_return( ExpressionNode *ctl ) { |
---|
[7880579] | 150 | std::list< Expression * > exps; |
---|
[7ecbb7e] | 151 | buildMoveList( ctl, exps ); |
---|
[8cc5cb0] | 152 | return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr ); |
---|
| 153 | } |
---|
[daf1af8] | 154 | |
---|
[8cc5cb0] | 155 | Statement *build_throw( ExpressionNode *ctl ) { |
---|
[7880579] | 156 | std::list< Expression * > exps; |
---|
[7ecbb7e] | 157 | buildMoveList( ctl, exps ); |
---|
[ac71a86] | 158 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
[daf1af8] | 159 | return new ThrowStmt( noLabels, ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr ); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | Statement *build_resume( ExpressionNode *ctl ) { |
---|
| 163 | std::list< Expression * > exps; |
---|
| 164 | buildMoveList( ctl, exps ); |
---|
| 165 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
| 166 | return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr ); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) { |
---|
[794c15b] | 170 | (void)ctl; |
---|
| 171 | (void)target; |
---|
| 172 | assertf( false, "resume at (non-local throw) is not yet supported," ); |
---|
[8cc5cb0] | 173 | } |
---|
[321f55d] | 174 | |
---|
[1d4580a] | 175 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) { |
---|
[046e04a] | 176 | std::list< CatchStmt * > branches; |
---|
| 177 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches ); |
---|
[4f147cc] | 178 | CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt)); |
---|
[7ecbb7e] | 179 | FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) ); |
---|
[1d4580a] | 180 | return new TryStmt( noLabels, tryBlock, branches, finallyBlock ); |
---|
| 181 | } |
---|
[ca78437] | 182 | Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) { |
---|
[7880579] | 183 | std::list< Statement * > branches; |
---|
[ca78437] | 184 | buildMoveList< Statement, StatementNode >( body, branches ); |
---|
[1d4580a] | 185 | assert( branches.size() == 1 ); |
---|
[ca78437] | 186 | return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() ); |
---|
[1d4580a] | 187 | } |
---|
| 188 | Statement *build_finally( StatementNode *stmt ) { |
---|
[7880579] | 189 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 190 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[1d4580a] | 191 | assert( branches.size() == 1 ); |
---|
[7880579] | 192 | return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) ); |
---|
[1d4580a] | 193 | } |
---|
| 194 | |
---|
[e82aa9df] | 195 | Statement *build_compound( StatementNode *first ) { |
---|
| 196 | CompoundStmt *cs = new CompoundStmt( noLabels ); |
---|
[7ecbb7e] | 197 | buildMoveList( first, cs->get_kids() ); |
---|
[b87a5ed] | 198 | return cs; |
---|
[51b7345] | 199 | } |
---|
| 200 | |
---|
[e82aa9df] | 201 | Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) { |
---|
[7f5566b] | 202 | std::list< Expression * > out, in; |
---|
| 203 | std::list< ConstantExpr * > clob; |
---|
[e82aa9df] | 204 | |
---|
[7ecbb7e] | 205 | buildMoveList( output, out ); |
---|
| 206 | buildMoveList( input, in ); |
---|
| 207 | buildMoveList( clobber, clob ); |
---|
[e82aa9df] | 208 | return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); |
---|
[7f5566b] | 209 | } |
---|
| 210 | |
---|
[51b7345] | 211 | // Local Variables: // |
---|
[b87a5ed] | 212 | // tab-width: 4 // |
---|
| 213 | // mode: c++ // |
---|
| 214 | // compile-command: "make install" // |
---|
[51b7345] | 215 | // End: // |
---|