[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
|
---|
[45040b61] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Mon Jan 31 18:52:35 2022
|
---|
| 13 | // Update Count : 23
|
---|
[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 |
|
---|
| 25 | template<typename StmtT>
|
---|
[45040b61] | 26 | const Stmt * hoist( const StmtT * stmt ) {
|
---|
[a488783] | 27 | // If no hoisting is needed, then make no changes.
|
---|
[45040b61] | 28 |
|
---|
| 29 | if ( stmt->inits.size() == 0 ) { // no declarations ?
|
---|
| 30 | // if ( /* no conditional declarations */ ... ) ...
|
---|
[a488783] | 31 | return stmt;
|
---|
[45040b61] | 32 | } // if
|
---|
| 33 |
|
---|
| 34 | // Put hoist declarations and modified statement in a compound statement.
|
---|
[a488783] | 35 |
|
---|
[45040b61] | 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
|
---|
[a488783] | 41 | block->kids.push_back( next );
|
---|
| 42 | }
|
---|
[45040b61] | 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 | // }
|
---|
[a488783] | 60 | return block;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[45040b61] | 63 | struct hoistControlDeclsCore {
|
---|
| 64 | // Statements with declarations in conditional.
|
---|
| 65 | const Stmt * postvisit( const IfStmt * stmt ) {
|
---|
| 66 | return hoist<IfStmt>( stmt );
|
---|
[a488783] | 67 | }
|
---|
[45040b61] | 68 | const Stmt * postvisit( const ForStmt * stmt ) {
|
---|
| 69 | return hoist<ForStmt>( stmt );
|
---|
[a488783] | 70 | }
|
---|
[45040b61] | 71 | const Stmt * postvisit( const WhileStmt * stmt ) {
|
---|
| 72 | return hoist<WhileStmt>( stmt );
|
---|
[a488783] | 73 | }
|
---|
| 74 | };
|
---|
| 75 |
|
---|
[45040b61] | 76 | // Hoist initialization out of for statements.
|
---|
| 77 | void hoistControlDecls( TranslationUnit & translationUnit ) {
|
---|
| 78 | Pass<hoistControlDeclsCore>::run( translationUnit );
|
---|
[a488783] | 79 | }
|
---|
| 80 |
|
---|
| 81 | } // namespace ControlStruct
|
---|
| 82 |
|
---|
| 83 | // Local Variables: //
|
---|
| 84 | // tab-width: 4 //
|
---|
| 85 | // mode: c++ //
|
---|
| 86 | // compile-command: "make install" //
|
---|
| 87 | // End: //
|
---|