[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 |
---|
[1db21619] | 11 | // Last Modified By : Peter A. Buhr |
---|
[c0aa336] | 12 | // Last Modified On : Thu Feb 2 22:16:40 2017 |
---|
| 13 | // Update Count : 327 |
---|
[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" |
---|
| 23 | #include "parseutility.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 ); |
---|
[321f55d] | 95 | assert( branches.size() >= 0 ); // 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 | } |
---|
| 154 | Statement *build_throw( ExpressionNode *ctl ) { |
---|
[7880579] | 155 | std::list< Expression * > exps; |
---|
[7ecbb7e] | 156 | buildMoveList( ctl, exps ); |
---|
[ac71a86] | 157 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
| 158 | return new ReturnStmt( noLabels, !exps.empty() ? exps.back() : nullptr, true ); |
---|
[8cc5cb0] | 159 | } |
---|
[321f55d] | 160 | |
---|
[1d4580a] | 161 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) { |
---|
[7880579] | 162 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 163 | buildMoveList< Statement, StatementNode >( catch_stmt, branches ); |
---|
[4f147cc] | 164 | CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt)); |
---|
[7ecbb7e] | 165 | FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) ); |
---|
[1d4580a] | 166 | return new TryStmt( noLabels, tryBlock, branches, finallyBlock ); |
---|
| 167 | } |
---|
| 168 | Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny ) { |
---|
[7880579] | 169 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 170 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[1d4580a] | 171 | assert( branches.size() == 1 ); |
---|
[7ecbb7e] | 172 | return new CatchStmt( noLabels, maybeMoveBuild< Declaration >(decl), branches.front(), catchAny ); |
---|
[1d4580a] | 173 | } |
---|
| 174 | Statement *build_finally( StatementNode *stmt ) { |
---|
[7880579] | 175 | std::list< Statement * > branches; |
---|
[7ecbb7e] | 176 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
[1d4580a] | 177 | assert( branches.size() == 1 ); |
---|
[7880579] | 178 | return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) ); |
---|
[1d4580a] | 179 | } |
---|
| 180 | |
---|
[e82aa9df] | 181 | Statement *build_compound( StatementNode *first ) { |
---|
| 182 | CompoundStmt *cs = new CompoundStmt( noLabels ); |
---|
[7ecbb7e] | 183 | buildMoveList( first, cs->get_kids() ); |
---|
[b87a5ed] | 184 | return cs; |
---|
[51b7345] | 185 | } |
---|
| 186 | |
---|
[e82aa9df] | 187 | Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) { |
---|
[7f5566b] | 188 | std::list< Expression * > out, in; |
---|
| 189 | std::list< ConstantExpr * > clob; |
---|
[e82aa9df] | 190 | |
---|
[7ecbb7e] | 191 | buildMoveList( output, out ); |
---|
| 192 | buildMoveList( input, in ); |
---|
| 193 | buildMoveList( clobber, clob ); |
---|
[e82aa9df] | 194 | return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); |
---|
[7f5566b] | 195 | } |
---|
| 196 | |
---|
[51b7345] | 197 | // Local Variables: // |
---|
[b87a5ed] | 198 | // tab-width: 4 // |
---|
| 199 | // mode: c++ // |
---|
| 200 | // compile-command: "make install" // |
---|
[51b7345] | 201 | // End: // |
---|