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
|
---|
11 | // Last Modified By : Andrew Beach
|
---|
12 | // Last Modified On : Fri Dec 3 15:34:00 2021
|
---|
13 | // Update Count : 0
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "HoistControlDecls.hpp"
|
---|
17 |
|
---|
18 | #include "AST/Decl.hpp"
|
---|
19 | #include "AST/Pass.hpp"
|
---|
20 | #include "AST/TranslationUnit.hpp"
|
---|
21 |
|
---|
22 | namespace ControlStruct {
|
---|
23 |
|
---|
24 | namespace {
|
---|
25 |
|
---|
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() ) {
|
---|
30 | return stmt;
|
---|
31 | }
|
---|
32 |
|
---|
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 ) {
|
---|
37 | block->kids.push_back( next );
|
---|
38 | }
|
---|
39 | mutStmt->inits.clear();
|
---|
40 | block->kids.push_back( mutStmt );
|
---|
41 | return block;
|
---|
42 | }
|
---|
43 |
|
---|
44 | struct HoistControlCore {
|
---|
45 | const ast::Stmt * postvisit( const ast::IfStmt * stmt ) {
|
---|
46 | return hoist<ast::IfStmt>( stmt );
|
---|
47 | }
|
---|
48 | const ast::Stmt * postvisit( const ast::ForStmt * stmt ) {
|
---|
49 | return hoist<ast::ForStmt>( stmt );
|
---|
50 | }
|
---|
51 | const ast::Stmt * postvisit( const ast::WhileStmt * stmt ) {
|
---|
52 | return hoist<ast::WhileStmt>( stmt );
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | } // namespace
|
---|
57 |
|
---|
58 | /// Hoist initialization out of for statements.
|
---|
59 | void hoistControlDecls( ast::TranslationUnit & translationUnit ) {
|
---|
60 | ast::Pass<HoistControlCore>::run( translationUnit );
|
---|
61 | }
|
---|
62 |
|
---|
63 | } // namespace ControlStruct
|
---|
64 |
|
---|
65 | // Local Variables: //
|
---|
66 | // tab-width: 4 //
|
---|
67 | // mode: c++ //
|
---|
68 | // compile-command: "make install" //
|
---|
69 | // End: //
|
---|