source: src/ControlStruct/HoistControlDecls.cpp @ b0845f9

Last change on this file since b0845f9 was 3b0bc16, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

change class name WhileStmt? to WhileDoStmt?, add else clause to WhileDoStmt? and ForStmt?, change names thenPart/ElsePart to then/else_

  • Property mode set to 100644
File size: 2.3 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 : Tue Feb  1 18:59:47 2022
13// Update Count     : 25
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
25template<typename StmtT>
26const Stmt * hoist( const StmtT * stmt ) {
27        // If no hoisting is needed, then make no changes.
28
29        if ( stmt->inits.size() == 0 ) {                                        // no declarations ?
30                // if ( /* no conditional declarations */ ...  ) ...
31                return stmt;
32        } // if
33
34        // Put hoist declarations and modified statement in a compound statement.
35
36        CompoundStmt * block = new CompoundStmt( stmt->location ); // create empty compound statement
37        //    {}
38
39        for ( const Stmt * next : stmt->inits ) {                       // link conditional declarations into compound
40                block->kids.push_back( next );
41        }
42        //    if ( int x = f(), y = g(); ... ) ...
43        // link declarations into compound statement
44        //    {
45        //         int x = f();
46        //         int y = g();
47        //    }
48
49        StmtT * mutStmt = mutate( stmt );                                       // create mutate handle to change statement
50        mutStmt->inits.clear();                                                         // remove declarations
51        //    if ( ... ) ...
52
53        block->kids.push_back( mutStmt );                                       // link modified statement into compound
54        //    {
55        //        int x = f();
56        //        int y = g();
57        //        if ( ... ) ...
58        //    }
59        return block;
60}
61
62struct hoistControlDeclsCore {
63        // Statements with declarations in conditional.
64        const Stmt * postvisit( const IfStmt * stmt ) {
65                return hoist<IfStmt>( stmt );
66        }
67        const Stmt * postvisit( const ForStmt * stmt ) {
68                return hoist<ForStmt>( stmt );
69        }
70        const Stmt * postvisit( const WhileDoStmt * stmt ) {
71                return hoist<WhileDoStmt>( stmt );
72        }
73};
74
75// Hoist initialization out of for statements.
76void hoistControlDecls( TranslationUnit & translationUnit ) {
77        Pass<hoistControlDeclsCore>::run( translationUnit );
78}
79
80} // namespace ControlStruct
81
82// Local Variables: //
83// tab-width: 4 //
84// mode: c++ //
85// compile-command: "make install" //
86// End: //
Note: See TracBrowser for help on using the repository browser.