Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/ForExprMutator.cc

    r0db6fc0 r8bafacc  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Tue Jul 14 12:14:44 2015
    13 // Update Count     : 10
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Aug 18 10:22:00 2017
     13// Update Count     : 12
    1414//
    1515
    16 #include "SynTree/Mutator.h"
    17 #include "SynTree/Statement.h"
     16#include <list>                 // for list, _List_iterator, list<>::iterator
     17
    1818#include "ForExprMutator.h"
     19#include "SynTree/Label.h"      // for Label
     20#include "SynTree/Statement.h"  // for Statement (ptr only), ForStmt, Compou...
    1921
    2022namespace ControlStruct {
     23        Statement *hoist( Statement *originalStmt, std::list<Statement *> &init ) {
     24                // If no hoisting is needed, skip:
     25                if ( 0 == init.size() ) {
     26                        return originalStmt;
     27                }
     28
     29                // Create compound statement, move initializers outside,
     30                // the resut of the original stays as is.
     31                CompoundStmt *block = new CompoundStmt( std::list< Label >() );
     32                std::list<Statement *> &stmts = block->get_kids();
     33                stmts.splice( stmts.end(), init );
     34
     35                // Add for to the new block.
     36                stmts.push_back( originalStmt );
     37                return block;
     38        }
     39
     40        Statement *ForExprMutator::postmutate( IfStmt *ifStmt ) {
     41                return hoist( ifStmt, ifStmt->initialization );
     42        }
    2143        Statement *ForExprMutator::postmutate( ForStmt *forStmt ) {
    2244                // hoist any initializer declarations to make them C89 (rather than C99)
    23                 std::list<Statement *> &init = forStmt->get_initialization();
    24                 if ( init.size() == 0 ) {
    25                         return forStmt;
    26                 } // if
    27 
    28                 // create compound statement, move initializers outside, leave _for_ as-is
    29                 CompoundStmt *block = new CompoundStmt( std::list< Label >() );
    30                 std::list<Statement *> &stmts = block->get_kids();
    31                 for ( std::list<Statement *>::iterator it = init.begin(); it != init.end(); ++it ) {
    32                         stmts.push_back( *it );
    33                 }       // for
    34 
    35                 // add for to the new block
    36                 stmts.push_back( forStmt );
    37                 forStmt->set_initialization( std::list<Statement *>() );
    38                 return block;
     45                return hoist( forStmt, forStmt->initialization );
    3946        }
    4047} // namespace ControlStruct
Note: See TracChangeset for help on using the changeset viewer.