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