| 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 | //
|
|---|
| 7 | // StatementNode.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 10 | // Created On : Sat May 16 14:59:41 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Tue Jul 11 21:23:15 2017
|
|---|
| 13 | // Update Count : 331
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 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
|
|---|
| 20 |
|
|---|
| 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;
|
|---|
| 30 |
|
|---|
| 31 | using namespace std;
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | StatementNode::StatementNode( DeclarationNode *decl ) {
|
|---|
| 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 );
|
|---|
| 43 | } // if
|
|---|
| 44 | } else {
|
|---|
| 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;
|
|---|
| 50 | } // if
|
|---|
| 51 | stmt.reset( new DeclStmt( noLabels, maybeMoveBuild< Declaration >(agg) ) );
|
|---|
| 52 | } // StatementNode::StatementNode
|
|---|
| 53 |
|
|---|
| 54 | StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
|
|---|
| 55 | StatementNode *prev = this;
|
|---|
| 56 | // find end of list and maintain previous pointer
|
|---|
| 57 | for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
|
|---|
| 58 | StatementNode *node = safe_dynamic_cast< StatementNode * >(curr);
|
|---|
| 59 | assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
|
|---|
| 60 | prev = curr;
|
|---|
| 61 | } // for
|
|---|
| 62 | // convert from StatementNode list to Statement list
|
|---|
| 63 | StatementNode *node = dynamic_cast< StatementNode * >(prev);
|
|---|
| 64 | std::list< Statement * > stmts;
|
|---|
| 65 | buildMoveList( stmt, stmts );
|
|---|
| 66 | // splice any new Statements to end of current Statements
|
|---|
| 67 | CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
|
|---|
| 68 | caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
|
|---|
| 69 | return this;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | Statement *build_expr( ExpressionNode *ctl ) {
|
|---|
| 73 | Expression *e = maybeMoveBuild< Expression >( ctl );
|
|---|
| 74 |
|
|---|
| 75 | if ( e )
|
|---|
| 76 | return new ExprStmt( noLabels, e );
|
|---|
| 77 | else
|
|---|
| 78 | return new NullStmt( noLabels );
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
|
|---|
| 82 | Statement *thenb, *elseb = 0;
|
|---|
| 83 | std::list< Statement * > branches;
|
|---|
| 84 | buildMoveList< Statement, StatementNode >( then_stmt, branches );
|
|---|
| 85 | assert( branches.size() == 1 );
|
|---|
| 86 | thenb = branches.front();
|
|---|
| 87 |
|
|---|
| 88 | if ( else_stmt ) {
|
|---|
| 89 | std::list< Statement * > branches;
|
|---|
| 90 | buildMoveList< Statement, StatementNode >( else_stmt, branches );
|
|---|
| 91 | assert( branches.size() == 1 );
|
|---|
| 92 | elseb = branches.front();
|
|---|
| 93 | } // if
|
|---|
| 94 | return new IfStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), thenb, elseb );
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
|
|---|
| 98 | std::list< Statement * > branches;
|
|---|
| 99 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
|---|
| 100 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
|
|---|
| 101 | return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
|
|---|
| 102 | }
|
|---|
| 103 | Statement *build_case( ExpressionNode *ctl ) {
|
|---|
| 104 | std::list< Statement * > branches;
|
|---|
| 105 | return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
|
|---|
| 106 | }
|
|---|
| 107 | Statement *build_default() {
|
|---|
| 108 | std::list< Statement * > branches;
|
|---|
| 109 | return new CaseStmt( noLabels, nullptr, branches, true );
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
|
|---|
| 113 | std::list< Statement * > branches;
|
|---|
| 114 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
|---|
| 115 | assert( branches.size() == 1 );
|
|---|
| 116 | return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
|
|---|
| 120 | std::list< Statement * > branches;
|
|---|
| 121 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
|---|
| 122 | assert( branches.size() == 1 );
|
|---|
| 123 |
|
|---|
| 124 | std::list< Statement * > init;
|
|---|
| 125 | if ( forctl->init != 0 ) {
|
|---|
| 126 | buildMoveList( forctl->init, init );
|
|---|
| 127 | } // if
|
|---|
| 128 |
|
|---|
| 129 | Expression *cond = 0;
|
|---|
| 130 | if ( forctl->condition != 0 )
|
|---|
| 131 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
|
|---|
| 132 |
|
|---|
| 133 | Expression *incr = 0;
|
|---|
| 134 | if ( forctl->change != 0 )
|
|---|
| 135 | incr = maybeMoveBuild< Expression >(forctl->change);
|
|---|
| 136 |
|
|---|
| 137 | delete forctl;
|
|---|
| 138 | return new ForStmt( noLabels, init, cond, incr, branches.front() );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 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;
|
|---|
| 149 | }
|
|---|
| 150 | Statement *build_computedgoto( ExpressionNode *ctl ) {
|
|---|
| 151 | return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | Statement *build_return( ExpressionNode *ctl ) {
|
|---|
| 155 | std::list< Expression * > exps;
|
|---|
| 156 | buildMoveList( ctl, exps );
|
|---|
| 157 | return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | Statement *build_throw( ExpressionNode *ctl ) {
|
|---|
| 161 | std::list< Expression * > exps;
|
|---|
| 162 | buildMoveList( ctl, exps );
|
|---|
| 163 | assertf( exps.size() < 2, "This means we are leaking memory");
|
|---|
| 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 ) {
|
|---|
| 175 | (void)ctl;
|
|---|
| 176 | (void)target;
|
|---|
| 177 | assertf( false, "resume at (non-local throw) is not yet supported," );
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
|
|---|
| 181 | std::list< CatchStmt * > branches;
|
|---|
| 182 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
|
|---|
| 183 | CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
|
|---|
| 184 | FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
|
|---|
| 185 | return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
|
|---|
| 186 | }
|
|---|
| 187 | Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
|
|---|
| 188 | std::list< Statement * > branches;
|
|---|
| 189 | buildMoveList< Statement, StatementNode >( body, branches );
|
|---|
| 190 | assert( branches.size() == 1 );
|
|---|
| 191 | return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
|
|---|
| 192 | }
|
|---|
| 193 | Statement *build_finally( StatementNode *stmt ) {
|
|---|
| 194 | std::list< Statement * > branches;
|
|---|
| 195 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
|---|
| 196 | assert( branches.size() == 1 );
|
|---|
| 197 | return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | Statement *build_compound( StatementNode *first ) {
|
|---|
| 201 | CompoundStmt *cs = new CompoundStmt( noLabels );
|
|---|
| 202 | buildMoveList( first, cs->get_kids() );
|
|---|
| 203 | return cs;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
|
|---|
| 207 | std::list< Expression * > out, in;
|
|---|
| 208 | std::list< ConstantExpr * > clob;
|
|---|
| 209 |
|
|---|
| 210 | buildMoveList( output, out );
|
|---|
| 211 | buildMoveList( input, in );
|
|---|
| 212 | buildMoveList( clobber, clob );
|
|---|
| 213 | return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | // Local Variables: //
|
|---|
| 217 | // tab-width: 4 //
|
|---|
| 218 | // mode: c++ //
|
|---|
| 219 | // compile-command: "make install" //
|
|---|
| 220 | // End: //
|
|---|