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

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

update documentation for hoisting of loop declarations

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