| [a488783] | 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 | // HoistControlDecls.cpp -- Desugar Cforall control structures. | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Andrew Beach | 
|---|
|  | 10 | // Created On       : Fri Dec  3 15:34:00 2021 | 
|---|
| [b6923b17] | 11 | // Last Modified By : Andrew Beach | 
|---|
|  | 12 | // Last Modified On : Wed Jul 24 12:00:00 2024 | 
|---|
|  | 13 | // Update Count     : 3 | 
|---|
| [a488783] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "HoistControlDecls.hpp" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "AST/Decl.hpp" | 
|---|
|  | 19 | #include "AST/Pass.hpp" | 
|---|
|  | 20 | #include "AST/TranslationUnit.hpp" | 
|---|
| [45040b61] | 21 | using namespace ast; | 
|---|
| [a488783] | 22 |  | 
|---|
|  | 23 | namespace ControlStruct { | 
|---|
|  | 24 |  | 
|---|
| [b6923b17] | 25 | namespace { | 
|---|
|  | 26 |  | 
|---|
| [a488783] | 27 | template<typename StmtT> | 
|---|
| [45040b61] | 28 | const Stmt * hoist( const StmtT * stmt ) { | 
|---|
| [b6923b17] | 29 | // If no hoisting is needed (no declaration), then make no changes. | 
|---|
|  | 30 | if ( stmt->inits.size() == 0 ) { | 
|---|
| [45040b61] | 31 | // if ( /* no conditional declarations */ ...  ) ... | 
|---|
| [a488783] | 32 | return stmt; | 
|---|
| [b6923b17] | 33 | } | 
|---|
| [a488783] | 34 |  | 
|---|
| [b6923b17] | 35 | StmtT * mutStmt = mutate( stmt ); | 
|---|
|  | 36 | CompoundStmt * block = new CompoundStmt( stmt->location ); | 
|---|
| [3b0bc16] | 37 | //    {} | 
|---|
| [45040b61] | 38 |  | 
|---|
| [b6923b17] | 39 | //    Label: if ( int x = f(), y = g(); ... ) ... | 
|---|
|  | 40 | // link declarations into compound statement | 
|---|
|  | 41 | for ( const Stmt * next : mutStmt->inits ) { | 
|---|
| [a488783] | 42 | block->kids.push_back( next ); | 
|---|
|  | 43 | } | 
|---|
| [45040b61] | 44 | //    { | 
|---|
|  | 45 | //         int x = f(); | 
|---|
|  | 46 | //         int y = g(); | 
|---|
|  | 47 | //    } | 
|---|
| [b6923b17] | 48 | mutStmt->inits.clear(); | 
|---|
|  | 49 | //    Label: if ( ... ) ... | 
|---|
| [45040b61] | 50 |  | 
|---|
| [b6923b17] | 51 | block->labels.swap( mutStmt->labels ); | 
|---|
|  | 52 | //    Label: { | 
|---|
|  | 53 | //        int x = f(); | 
|---|
|  | 54 | //        int y = g(); | 
|---|
|  | 55 | //    } | 
|---|
| [45040b61] | 56 | //    if ( ... ) ... | 
|---|
|  | 57 |  | 
|---|
| [b6923b17] | 58 | block->kids.push_back( mutStmt ); | 
|---|
|  | 59 | //    Label: { | 
|---|
| [45040b61] | 60 | //        int x = f(); | 
|---|
|  | 61 | //        int y = g(); | 
|---|
|  | 62 | //        if ( ... ) ... | 
|---|
|  | 63 | //    } | 
|---|
| [a488783] | 64 | return block; | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
| [45040b61] | 67 | struct hoistControlDeclsCore { | 
|---|
|  | 68 | // Statements with declarations in conditional. | 
|---|
|  | 69 | const Stmt * postvisit( const IfStmt * stmt ) { | 
|---|
|  | 70 | return hoist<IfStmt>( stmt ); | 
|---|
| [a488783] | 71 | } | 
|---|
| [45040b61] | 72 | const Stmt * postvisit( const ForStmt * stmt ) { | 
|---|
|  | 73 | return hoist<ForStmt>( stmt ); | 
|---|
| [a488783] | 74 | } | 
|---|
| [3b0bc16] | 75 | const Stmt * postvisit( const WhileDoStmt * stmt ) { | 
|---|
|  | 76 | return hoist<WhileDoStmt>( stmt ); | 
|---|
| [a488783] | 77 | } | 
|---|
|  | 78 | }; | 
|---|
|  | 79 |  | 
|---|
| [b6923b17] | 80 | } // namespace | 
|---|
|  | 81 |  | 
|---|
| [45040b61] | 82 | // Hoist initialization out of for statements. | 
|---|
|  | 83 | void hoistControlDecls( TranslationUnit & translationUnit ) { | 
|---|
|  | 84 | Pass<hoistControlDeclsCore>::run( translationUnit ); | 
|---|
| [a488783] | 85 | } | 
|---|
|  | 86 |  | 
|---|
|  | 87 | } // namespace ControlStruct | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // Local Variables: // | 
|---|
|  | 90 | // tab-width: 4 // | 
|---|
|  | 91 | // mode: c++ // | 
|---|
|  | 92 | // compile-command: "make install" // | 
|---|
|  | 93 | // End: // | 
|---|