source: src/ControlStruct/HoistControlDecls.cpp @ 85a2c3f

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 85a2c3f 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
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
[45040b61]11// Last Modified By : Peter A. Buhr
[3b0bc16]12// Last Modified On : Tue Feb  1 18:59:47 2022
13// Update Count     : 25
[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
25template<typename StmtT>
[45040b61]26const Stmt * hoist( const StmtT * stmt ) {
[a488783]27        // If no hoisting is needed, then make no changes.
[45040b61]28
29        if ( stmt->inits.size() == 0 ) {                                        // no declarations ?
30                // if ( /* no conditional declarations */ ...  ) ...
[a488783]31                return stmt;
[45040b61]32        } // if
33
34        // Put hoist declarations and modified statement in a compound statement.
[a488783]35
[45040b61]36        CompoundStmt * block = new CompoundStmt( stmt->location ); // create empty compound statement
[3b0bc16]37        //    {}
[45040b61]38
39        for ( const Stmt * next : stmt->inits ) {                       // link conditional declarations into compound
[a488783]40                block->kids.push_back( next );
41        }
[45040b61]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        //    }
[a488783]59        return block;
60}
61
[45040b61]62struct hoistControlDeclsCore {
63        // Statements with declarations in conditional.
64        const Stmt * postvisit( const IfStmt * stmt ) {
65                return hoist<IfStmt>( stmt );
[a488783]66        }
[45040b61]67        const Stmt * postvisit( const ForStmt * stmt ) {
68                return hoist<ForStmt>( stmt );
[a488783]69        }
[3b0bc16]70        const Stmt * postvisit( const WhileDoStmt * stmt ) {
71                return hoist<WhileDoStmt>( stmt );
[a488783]72        }
73};
74
[45040b61]75// Hoist initialization out of for statements.
76void hoistControlDecls( TranslationUnit & translationUnit ) {
77        Pass<hoistControlDeclsCore>::run( translationUnit );
[a488783]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.