| 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 -- Transform from parse data-structures to AST data-structures, usually deleting the parse
 | 
|---|
| 8 | //     data-structure after the transformation.
 | 
|---|
| 9 | //
 | 
|---|
| 10 | // Author           : Rodolfo G. Esteves
 | 
|---|
| 11 | // Created On       : Sat May 16 14:59:41 2015
 | 
|---|
| 12 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 13 | // Last Modified On : Wed Feb  2 20:29:30 2022
 | 
|---|
| 14 | // Update Count     : 425
 | 
|---|
| 15 | //
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <cassert>                 // for assert, strict_dynamic_cast, assertf
 | 
|---|
| 18 | #include <list>                    // for list
 | 
|---|
| 19 | #include <memory>                  // for unique_ptr
 | 
|---|
| 20 | #include <string>                  // for string
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Common/SemanticError.h"  // for SemanticError
 | 
|---|
| 23 | #include "Common/utility.h"        // for maybeMoveBuild, maybeBuild
 | 
|---|
| 24 | #include "ParseNode.h"             // for StatementNode, ExpressionNode, bui...
 | 
|---|
| 25 | #include "SynTree/Expression.h"    // for Expression, ConstantExpr
 | 
|---|
| 26 | #include "SynTree/Label.h"         // for Label, noLabels
 | 
|---|
| 27 | #include "SynTree/Declaration.h"
 | 
|---|
| 28 | #include "SynTree/Statement.h"     // for Statement, BranchStmt, CaseStmt
 | 
|---|
| 29 | #include "parserutility.h"         // for notZeroExpr
 | 
|---|
| 30 | 
 | 
|---|
| 31 | class Declaration;
 | 
|---|
| 32 | 
 | 
|---|
| 33 | using namespace std;
 | 
|---|
| 34 | 
 | 
|---|
| 35 | 
 | 
|---|
| 36 | StatementNode::StatementNode( DeclarationNode * decl ) {
 | 
|---|
| 37 |         assert( decl );
 | 
|---|
| 38 |         DeclarationNode * agg = decl->extractAggregate();
 | 
|---|
| 39 |         if ( agg ) {
 | 
|---|
| 40 |                 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
 | 
|---|
| 41 |                 set_next( nextStmt );
 | 
|---|
| 42 |                 if ( decl->get_next() ) {
 | 
|---|
| 43 |                         get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
 | 
|---|
| 44 |                         decl->set_next( 0 );
 | 
|---|
| 45 |                 } // if
 | 
|---|
| 46 |         } else {
 | 
|---|
| 47 |                 if ( decl->get_next() ) {
 | 
|---|
| 48 |                         set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
 | 
|---|
| 49 |                         decl->set_next( 0 );
 | 
|---|
| 50 |                 } // if
 | 
|---|
| 51 |                 agg = decl;
 | 
|---|
| 52 |         } // if
 | 
|---|
| 53 |         stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) );
 | 
|---|
| 54 | } // StatementNode::StatementNode
 | 
|---|
| 55 | 
 | 
|---|
| 56 | StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
 | 
|---|
| 57 |         StatementNode * prev = this;
 | 
|---|
| 58 |         // find end of list and maintain previous pointer
 | 
|---|
| 59 |         for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
 | 
|---|
| 60 |                 StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
 | 
|---|
| 61 |                 assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
 | 
|---|
| 62 |                 prev = curr;
 | 
|---|
| 63 |         } // for
 | 
|---|
| 64 |         // convert from StatementNode list to Statement list
 | 
|---|
| 65 |         StatementNode * node = dynamic_cast< StatementNode * >(prev);
 | 
|---|
| 66 |         list< Statement * > stmts;
 | 
|---|
| 67 |         buildMoveList( stmt, stmts );
 | 
|---|
| 68 |         // splice any new Statements to end of current Statements
 | 
|---|
| 69 |         CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
 | 
|---|
| 70 |         caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
 | 
|---|
| 71 |         return this;
 | 
|---|
| 72 | } // StatementNode::append_last_case
 | 
|---|
| 73 | 
 | 
|---|
| 74 | Statement * build_expr( ExpressionNode * ctl ) {
 | 
|---|
| 75 |         Expression * e = maybeMoveBuild< Expression >( ctl );
 | 
|---|
| 76 | 
 | 
|---|
| 77 |         if ( e ) return new ExprStmt( e );
 | 
|---|
| 78 |         else return new NullStmt();
 | 
|---|
| 79 | } // build_expr
 | 
|---|
| 80 | 
 | 
|---|
| 81 | Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
 | 
|---|
| 82 |         if ( ctl->init != 0 ) {
 | 
|---|
| 83 |                 buildMoveList( ctl->init, init );
 | 
|---|
| 84 |         } // if
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         Expression * cond = nullptr;
 | 
|---|
| 87 |         if ( ctl->condition ) {
 | 
|---|
| 88 |                 // compare the provided condition against 0
 | 
|---|
| 89 |                 cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
 | 
|---|
| 90 |         } else {
 | 
|---|
| 91 |                 for ( Statement * stmt : init ) {
 | 
|---|
| 92 |                         // build the && of all of the declared variables compared against 0
 | 
|---|
| 93 |                         DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
 | 
|---|
| 94 |                         DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
 | 
|---|
| 95 |                         Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
 | 
|---|
| 96 |                         cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
 | 
|---|
| 97 |                 }
 | 
|---|
| 98 |         }
 | 
|---|
| 99 |         delete ctl;
 | 
|---|
| 100 |         return cond;
 | 
|---|
| 101 | } // build_if_control
 | 
|---|
| 102 | 
 | 
|---|
| 103 | Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ) {
 | 
|---|
| 104 |         list< Statement * > astinit;                                            // maybe empty
 | 
|---|
| 105 |         Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         Statement * astthen, * astelse = nullptr;
 | 
|---|
| 108 |         list< Statement * > aststmt;
 | 
|---|
| 109 |         buildMoveList< Statement, StatementNode >( then, aststmt );
 | 
|---|
| 110 |         assert( aststmt.size() == 1 );
 | 
|---|
| 111 |         astthen = aststmt.front();
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         if ( else_ ) {
 | 
|---|
| 114 |                 list< Statement * > aststmt;
 | 
|---|
| 115 |                 buildMoveList< Statement, StatementNode >( else_, aststmt );
 | 
|---|
| 116 |                 assert( aststmt.size() == 1 );
 | 
|---|
| 117 |                 astelse = aststmt.front();
 | 
|---|
| 118 |         } // if
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         return new IfStmt( astcond, astthen, astelse, astinit );
 | 
|---|
| 121 | } // build_if
 | 
|---|
| 122 | 
 | 
|---|
| 123 | Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
 | 
|---|
| 124 |         list< Statement * > aststmt;
 | 
|---|
| 125 |         buildMoveList< Statement, StatementNode >( stmt, aststmt );
 | 
|---|
| 126 |         if ( ! isSwitch ) {                                                                     // choose statement
 | 
|---|
| 127 |                 for ( Statement * stmt : aststmt ) {
 | 
|---|
| 128 |                         CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
 | 
|---|
| 129 |                         if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
 | 
|---|
| 130 |                                 CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
 | 
|---|
| 131 |                                 block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
 | 
|---|
| 132 |                         } // if
 | 
|---|
| 133 |                 } // for
 | 
|---|
| 134 |         } // if
 | 
|---|
| 135 |         // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
 | 
|---|
| 136 |         return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
 | 
|---|
| 137 | } // build_switch
 | 
|---|
| 138 | 
 | 
|---|
| 139 | Statement * build_case( ExpressionNode * ctl ) {
 | 
|---|
| 140 |         return new CaseStmt( maybeMoveBuild< Expression >(ctl), {} ); // stmt starts empty and then added to
 | 
|---|
| 141 | } // build_case
 | 
|---|
| 142 | 
 | 
|---|
| 143 | Statement * build_default() {
 | 
|---|
| 144 |         return new CaseStmt( nullptr, {}, true );                       // stmt starts empty and then added to
 | 
|---|
| 145 | } // build_default
 | 
|---|
| 146 | 
 | 
|---|
| 147 | Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
 | 
|---|
| 148 |         list< Statement * > astinit;                                            // maybe empty
 | 
|---|
| 149 |         Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
 | 
|---|
| 150 | 
 | 
|---|
| 151 |         list< Statement * > aststmt;                                            // loop body, compound created if empty
 | 
|---|
| 152 |         buildMoveList< Statement, StatementNode >( stmt, aststmt );
 | 
|---|
| 153 |         assert( aststmt.size() == 1 );
 | 
|---|
| 154 | 
 | 
|---|
| 155 |         list< Statement * > astelse;                                            // else clause, maybe empty
 | 
|---|
| 156 |         buildMoveList< Statement, StatementNode >( else_, astelse );
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
 | 
|---|
| 159 | } // build_while
 | 
|---|
| 160 | 
 | 
|---|
| 161 | Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
 | 
|---|
| 162 |         list< Statement * > aststmt;                                            // loop body, compound created if empty
 | 
|---|
| 163 |         buildMoveList< Statement, StatementNode >( stmt, aststmt );
 | 
|---|
| 164 |         assert( aststmt.size() == 1 );                                          // compound created if empty
 | 
|---|
| 165 | 
 | 
|---|
| 166 |         list< Statement * > astelse;                                            // else clause, maybe empty
 | 
|---|
| 167 |         buildMoveList< Statement, StatementNode >( else_, astelse );
 | 
|---|
| 168 | 
 | 
|---|
| 169 |         // do-while cannot have declarations in the contitional, so init is always empty
 | 
|---|
| 170 |         return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), {}, true );
 | 
|---|
| 171 | } // build_do_while
 | 
|---|
| 172 | 
 | 
|---|
| 173 | Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
 | 
|---|
| 174 |         list< Statement * > astinit;                                            // maybe empty
 | 
|---|
| 175 |         buildMoveList( forctl->init, astinit );
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         Expression * astcond = nullptr;                                         // maybe empty
 | 
|---|
| 178 |         astcond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
 | 
|---|
| 179 | 
 | 
|---|
| 180 |         Expression * astincr = nullptr;                                         // maybe empty
 | 
|---|
| 181 |         astincr = maybeMoveBuild< Expression >(forctl->change);
 | 
|---|
| 182 |         delete forctl;
 | 
|---|
| 183 | 
 | 
|---|
| 184 |         list< Statement * > aststmt;                                            // loop body, compound created if empty
 | 
|---|
| 185 |         buildMoveList< Statement, StatementNode >( stmt, aststmt );
 | 
|---|
| 186 |         assert( aststmt.size() == 1 );
 | 
|---|
| 187 | 
 | 
|---|
| 188 |         list< Statement * > astelse;                                            // else clause, maybe empty
 | 
|---|
| 189 |         buildMoveList< Statement, StatementNode >( else_, astelse );
 | 
|---|
| 190 | 
 | 
|---|
| 191 |         return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
 | 
|---|
| 192 | } // build_for
 | 
|---|
| 193 | 
 | 
|---|
| 194 | Statement * build_branch( BranchStmt::Type kind ) {
 | 
|---|
| 195 |         Statement * ret = new BranchStmt( "", kind );
 | 
|---|
| 196 |         return ret;
 | 
|---|
| 197 | } // build_branch
 | 
|---|
| 198 | 
 | 
|---|
| 199 | Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
 | 
|---|
| 200 |         Statement * ret = new BranchStmt( * identifier, kind );
 | 
|---|
| 201 |         delete identifier;                                                                      // allocated by lexer
 | 
|---|
| 202 |         return ret;
 | 
|---|
| 203 | } // build_branch
 | 
|---|
| 204 | 
 | 
|---|
| 205 | Statement * build_computedgoto( ExpressionNode * ctl ) {
 | 
|---|
| 206 |         return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
 | 
|---|
| 207 | } // build_computedgoto
 | 
|---|
| 208 | 
 | 
|---|
| 209 | Statement * build_return( ExpressionNode * ctl ) {
 | 
|---|
| 210 |         list< Expression * > exps;
 | 
|---|
| 211 |         buildMoveList( ctl, exps );
 | 
|---|
| 212 |         return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
 | 
|---|
| 213 | } // build_return
 | 
|---|
| 214 | 
 | 
|---|
| 215 | Statement * build_throw( ExpressionNode * ctl ) {
 | 
|---|
| 216 |         list< Expression * > exps;
 | 
|---|
| 217 |         buildMoveList( ctl, exps );
 | 
|---|
| 218 |         assertf( exps.size() < 2, "CFA internal error: leaking memory" );
 | 
|---|
| 219 |         return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
 | 
|---|
| 220 | } // build_throw
 | 
|---|
| 221 | 
 | 
|---|
| 222 | Statement * build_resume( ExpressionNode * ctl ) {
 | 
|---|
| 223 |         list< Expression * > exps;
 | 
|---|
| 224 |         buildMoveList( ctl, exps );
 | 
|---|
| 225 |         assertf( exps.size() < 2, "CFA internal error: leaking memory" );
 | 
|---|
| 226 |         return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
 | 
|---|
| 227 | } // build_resume
 | 
|---|
| 228 | 
 | 
|---|
| 229 | Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
 | 
|---|
| 230 |         (void)ctl;
 | 
|---|
| 231 |         (void)target;
 | 
|---|
| 232 |         assertf( false, "resume at (non-local throw) is not yet supported," );
 | 
|---|
| 233 | } // build_resume_at
 | 
|---|
| 234 | 
 | 
|---|
| 235 | Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ) {
 | 
|---|
| 236 |         list< CatchStmt * > aststmt;
 | 
|---|
| 237 |         buildMoveList< CatchStmt, StatementNode >( catch_, aststmt );
 | 
|---|
| 238 |         CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_));
 | 
|---|
| 239 |         FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_) );
 | 
|---|
| 240 |         return new TryStmt( tryBlock, aststmt, finallyBlock );
 | 
|---|
| 241 | } // build_try
 | 
|---|
| 242 | 
 | 
|---|
| 243 | Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
 | 
|---|
| 244 |         list< Statement * > aststmt;
 | 
|---|
| 245 |         buildMoveList< Statement, StatementNode >( body, aststmt );
 | 
|---|
| 246 |         assert( aststmt.size() == 1 );
 | 
|---|
| 247 |         return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), aststmt.front() );
 | 
|---|
| 248 | } // build_catch
 | 
|---|
| 249 | 
 | 
|---|
| 250 | Statement * build_finally( StatementNode * stmt ) {
 | 
|---|
| 251 |         list< Statement * > aststmt;
 | 
|---|
| 252 |         buildMoveList< Statement, StatementNode >( stmt, aststmt );
 | 
|---|
| 253 |         assert( aststmt.size() == 1 );
 | 
|---|
| 254 |         return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
 | 
|---|
| 255 | } // build_finally
 | 
|---|
| 256 | 
 | 
|---|
| 257 | SuspendStmt * build_suspend( StatementNode * then, SuspendStmt::Type type ) {
 | 
|---|
| 258 |         auto node = new SuspendStmt();
 | 
|---|
| 259 | 
 | 
|---|
| 260 |         node->type = type;
 | 
|---|
| 261 | 
 | 
|---|
| 262 |         list< Statement * > stmts;
 | 
|---|
| 263 |         buildMoveList< Statement, StatementNode >( then, stmts );
 | 
|---|
| 264 |         if(!stmts.empty()) {
 | 
|---|
| 265 |                 assert( stmts.size() == 1 );
 | 
|---|
| 266 |                 node->then = dynamic_cast< CompoundStmt * >( stmts.front() );
 | 
|---|
| 267 |         }
 | 
|---|
| 268 | 
 | 
|---|
| 269 |         return node;
 | 
|---|
| 270 | }
 | 
|---|
| 271 | 
 | 
|---|
| 272 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
 | 
|---|
| 273 |         auto node = new WaitForStmt();
 | 
|---|
| 274 | 
 | 
|---|
| 275 |         WaitForStmt::Target target;
 | 
|---|
| 276 |         target.function = maybeBuild<Expression>( targetExpr );
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
 | 
|---|
| 279 |         targetExpr->set_next( nullptr );
 | 
|---|
| 280 |         buildMoveList< Expression >( next, target.arguments );
 | 
|---|
| 281 | 
 | 
|---|
| 282 |         delete targetExpr;
 | 
|---|
| 283 | 
 | 
|---|
| 284 |         node->clauses.push_back( WaitForStmt::Clause{
 | 
|---|
| 285 |                 target,
 | 
|---|
| 286 |                 maybeMoveBuild<Statement >( stmt ),
 | 
|---|
| 287 |                 notZeroExpr( maybeMoveBuild<Expression>( when ) )
 | 
|---|
| 288 |         });
 | 
|---|
| 289 | 
 | 
|---|
| 290 |         return node;
 | 
|---|
| 291 | } // build_waitfor
 | 
|---|
| 292 | 
 | 
|---|
| 293 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
 | 
|---|
| 294 |         WaitForStmt::Target target;
 | 
|---|
| 295 |         target.function = maybeBuild<Expression>( targetExpr );
 | 
|---|
| 296 | 
 | 
|---|
| 297 |         ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
 | 
|---|
| 298 |         targetExpr->set_next( nullptr );
 | 
|---|
| 299 |         buildMoveList< Expression >( next, target.arguments );
 | 
|---|
| 300 | 
 | 
|---|
| 301 |         delete targetExpr;
 | 
|---|
| 302 | 
 | 
|---|
| 303 |         node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
 | 
|---|
| 304 |                 std::move( target ),
 | 
|---|
| 305 |                 maybeMoveBuild<Statement >( stmt ),
 | 
|---|
| 306 |                 notZeroExpr( maybeMoveBuild<Expression>( when ) )
 | 
|---|
| 307 |         });
 | 
|---|
| 308 | 
 | 
|---|
| 309 |         return node;
 | 
|---|
| 310 | } // build_waitfor
 | 
|---|
| 311 | 
 | 
|---|
| 312 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
 | 
|---|
| 313 |         auto node = new WaitForStmt();
 | 
|---|
| 314 | 
 | 
|---|
| 315 |         if( timeout ) {
 | 
|---|
| 316 |                 node->timeout.time      = maybeMoveBuild<Expression>( timeout );
 | 
|---|
| 317 |                 node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
 | 
|---|
| 318 |                 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
 | 
|---|
| 319 |         } else {
 | 
|---|
| 320 |                 node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
 | 
|---|
| 321 |                 node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
 | 
|---|
| 322 |         } // if
 | 
|---|
| 323 | 
 | 
|---|
| 324 |         return node;
 | 
|---|
| 325 | } // build_waitfor_timeout
 | 
|---|
| 326 | 
 | 
|---|
| 327 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_, ExpressionNode * else_when ) {
 | 
|---|
| 328 |         auto node = new WaitForStmt();
 | 
|---|
| 329 | 
 | 
|---|
| 330 |         node->timeout.time      = maybeMoveBuild<Expression>( timeout );
 | 
|---|
| 331 |         node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
 | 
|---|
| 332 |         node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
 | 
|---|
| 333 | 
 | 
|---|
| 334 |         node->orelse.statement  = maybeMoveBuild<Statement >( else_ );
 | 
|---|
| 335 |         node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
 | 
|---|
| 336 | 
 | 
|---|
| 337 |         return node;
 | 
|---|
| 338 | } // build_waitfor_timeout
 | 
|---|
| 339 | 
 | 
|---|
| 340 | Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
 | 
|---|
| 341 |         list< Expression * > e;
 | 
|---|
| 342 |         buildMoveList( exprs, e );
 | 
|---|
| 343 |         Statement * s = maybeMoveBuild<Statement>( stmt );
 | 
|---|
| 344 |         return new DeclStmt( new WithStmt( e, s ) );
 | 
|---|
| 345 | } // build_with
 | 
|---|
| 346 | 
 | 
|---|
| 347 | Statement * build_compound( StatementNode * first ) {
 | 
|---|
| 348 |         CompoundStmt * cs = new CompoundStmt();
 | 
|---|
| 349 |         buildMoveList( first, cs->get_kids() );
 | 
|---|
| 350 |         return cs;
 | 
|---|
| 351 | } // build_compound
 | 
|---|
| 352 | 
 | 
|---|
| 353 | // A single statement in a control structure is always converted to a compound statement so subsequent generated code
 | 
|---|
| 354 | // can be placed within this compound statement. Otherwise, code generation has to constantly check for a single
 | 
|---|
| 355 | // statement and wrap it into a compound statement to insert additional code. Hence, all control structures have a
 | 
|---|
| 356 | // conical form for code generation.
 | 
|---|
| 357 | StatementNode * maybe_build_compound( StatementNode * first ) {
 | 
|---|
| 358 |         // Optimization: if the control-structure statement is a compound statement, do not wrap it.
 | 
|---|
| 359 |         // e.g., if (...) {...} do not wrap the existing compound statement.
 | 
|---|
| 360 |         if ( ! dynamic_cast<CompoundStmt *>( first->stmt.get() ) ) { // unique_ptr
 | 
|---|
| 361 |                 CompoundStmt * cs = new CompoundStmt();
 | 
|---|
| 362 |                 buildMoveList( first, cs->get_kids() );
 | 
|---|
| 363 |                 return new StatementNode( cs );
 | 
|---|
| 364 |         } // if
 | 
|---|
| 365 |         return first;
 | 
|---|
| 366 | } // maybe_build_compound
 | 
|---|
| 367 | 
 | 
|---|
| 368 | Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
 | 
|---|
| 369 |         list< Expression * > out, in;
 | 
|---|
| 370 |         list< ConstantExpr * > clob;
 | 
|---|
| 371 | 
 | 
|---|
| 372 |         buildMoveList( output, out );
 | 
|---|
| 373 |         buildMoveList( input, in );
 | 
|---|
| 374 |         buildMoveList( clobber, clob );
 | 
|---|
| 375 |         return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
 | 
|---|
| 376 | } // build_asm
 | 
|---|
| 377 | 
 | 
|---|
| 378 | Statement * build_directive( string * directive ) {
 | 
|---|
| 379 |         return new DirectiveStmt( *directive );
 | 
|---|
| 380 | } // build_directive
 | 
|---|
| 381 | 
 | 
|---|
| 382 | Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
 | 
|---|
| 383 |         list< Expression * > expList;
 | 
|---|
| 384 |         buildMoveList( exprs, expList );
 | 
|---|
| 385 |         Statement * body = maybeMoveBuild<Statement>( stmt );
 | 
|---|
| 386 |         return new MutexStmt( body, expList );
 | 
|---|
| 387 | } // build_mutex
 | 
|---|
| 388 | 
 | 
|---|
| 389 | // Local Variables: //
 | 
|---|
| 390 | // tab-width: 4 //
 | 
|---|
| 391 | // mode: c++ //
 | 
|---|
| 392 | // compile-command: "make install" //
 | 
|---|
| 393 | // End: //
 | 
|---|