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