Changeset 45040b61


Ignore:
Timestamp:
Jan 31, 2022, 10:26:13 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
51ec1ab
Parents:
3a4732f
Message:

formatting, remove anonymous namespace

Location:
src/ControlStruct
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/HoistControlDecls.cpp

    r3a4732f r45040b61  
    99// Author           : Andrew Beach
    1010// Created On       : Fri Dec  3 15:34:00 2021
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Dec  3 15:34:00 2021
    13 // Update Count     : 0
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 18:52:35 2022
     13// Update Count     : 23
    1414//
    1515
     
    1919#include "AST/Pass.hpp"
    2020#include "AST/TranslationUnit.hpp"
     21using namespace ast;
    2122
    2223namespace ControlStruct {
    2324
    24 namespace {
     25template<typename StmtT>
     26const Stmt * hoist( const StmtT * stmt ) {
     27        // If no hoisting is needed, then make no changes.
    2528
    26 template<typename StmtT>
    27 const ast::Stmt * hoist( const StmtT * stmt ) {
    28         // If no hoisting is needed, then make no changes.
    29         if ( 0 == stmt->inits.size() ) {
     29        if ( stmt->inits.size() == 0 ) {                                        // no declarations ?
     30                // if ( /* no conditional declarations */ ...  ) ...
    3031                return stmt;
    31         }
     32        } // if
    3233
    33         // Put initializers and the old statement in the compound statement.
    34         ast::CompoundStmt * block = new ast::CompoundStmt( stmt->location );
    35         StmtT * mutStmt = ast::mutate( stmt );
    36         for ( const ast::Stmt * next : mutStmt->inits ) {
     34        // Put hoist declarations and modified statement in a compound statement.
     35
     36        CompoundStmt * block = new CompoundStmt( stmt->location ); // create empty compound statement
     37        //    {
     38        //    }
     39
     40        for ( const Stmt * next : stmt->inits ) {                       // link conditional declarations into compound
    3741                block->kids.push_back( next );
    3842        }
    39         mutStmt->inits.clear();
    40         block->kids.push_back( mutStmt );
     43        //    if ( int x = f(), y = g(); ... ) ...
     44        // link declarations into compound statement
     45        //    {
     46        //         int x = f();
     47        //         int y = g();
     48        //    }
     49
     50        StmtT * mutStmt = mutate( stmt );                                       // create mutate handle to change statement
     51        mutStmt->inits.clear();                                                         // remove declarations
     52        //    if ( ... ) ...
     53
     54        block->kids.push_back( mutStmt );                                       // link modified statement into compound
     55        //    {
     56        //        int x = f();
     57        //        int y = g();
     58        //        if ( ... ) ...
     59        //    }
    4160        return block;
    4261}
    4362
    44 struct HoistControlCore {
    45         const ast::Stmt * postvisit( const ast::IfStmt * stmt ) {
    46                 return hoist<ast::IfStmt>( stmt );
     63struct hoistControlDeclsCore {
     64        // Statements with declarations in conditional.
     65        const Stmt * postvisit( const IfStmt * stmt ) {
     66                return hoist<IfStmt>( stmt );
    4767        }
    48         const ast::Stmt * postvisit( const ast::ForStmt * stmt ) {
    49                 return hoist<ast::ForStmt>( stmt );
     68        const Stmt * postvisit( const ForStmt * stmt ) {
     69                return hoist<ForStmt>( stmt );
    5070        }
    51         const ast::Stmt * postvisit( const ast::WhileStmt * stmt ) {
    52                 return hoist<ast::WhileStmt>( stmt );
     71        const Stmt * postvisit( const WhileStmt * stmt ) {
     72                return hoist<WhileStmt>( stmt );
    5373        }
    5474};
    5575
    56 } // namespace
    57 
    58 /// Hoist initialization out of for statements.
    59 void hoistControlDecls( ast::TranslationUnit & translationUnit ) {
    60         ast::Pass<HoistControlCore>::run( translationUnit );
     76// Hoist initialization out of for statements.
     77void hoistControlDecls( TranslationUnit & translationUnit ) {
     78        Pass<hoistControlDeclsCore>::run( translationUnit );
    6179}
    6280
  • src/ControlStruct/HoistControlDecls.hpp

    r3a4732f r45040b61  
    99// Author           : Andrew Beach
    1010// Created On       : Fri Dec  3 15:31:00 2021
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Dec  3 15:31:00 2021
    13 // Update Count     : 0
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:25:07 2022
     13// Update Count     : 3
    1414//
    1515
     
    1717
    1818namespace ast {
    19         class TranslationUnit;
     19class TranslationUnit;
    2020}
    2121
    2222namespace ControlStruct {
    23 
    24 /// Hoist initialization out of control flow statements.
     23// Hoist declarations out of control flow statements into compound statement.
    2524void hoistControlDecls( ast::TranslationUnit & translationUnit );
    26 
    2725} // namespace ControlStruct
    2826
Note: See TracChangeset for help on using the changeset viewer.