source: src/ControlStruct/HoistControlDecls.cpp@ 43b6516

Last change on this file since 43b6516 was a783c74, checked in by Peter A. Buhr <pabuhr@…>, 3 weeks ago

update documentation for hoisting of loop declarations

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