source: src/ControlStruct/MLEMutator.h @ cca568e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since cca568e was 9bdb8b7, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

allow labelled break from within the body of a try statement

  • Property mode set to 100644
File size: 4.6 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// MLEMutator.h --
8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Oct 22 17:22:47 2019
13// Update Count     : 45
14//
15
16#pragma once
17
18#include <list>                    // for list
19#include <map>                     // for map
20#include <string>                  // for string
21#include <set>                     // for unordered_set
22
23#include "Common/PassVisitor.h"
24#include "Common/SemanticError.h"  // for SemanticError
25#include "SynTree/Label.h"         // for Label
26#include "SynTree/Mutator.h"       // for Mutator
27#include "SynTree/SynTree.h"       // for Visitor Nodes
28
29namespace ControlStruct {
30        class LabelGenerator;
31
32        class MLEMutator : public WithVisitorRef<MLEMutator>, public WithShortCircuiting, public WithGuards {
33          public:
34                class Entry;
35                MLEMutator( std::map<Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {}
36                ~MLEMutator();
37
38                void premutate( CompoundStmt *cmpndStmt );
39                Statement * postmutate( BranchStmt *branchStmt ) throw ( SemanticErrorException );
40                void premutate( WhileStmt *whileStmt );
41                Statement * postmutate( WhileStmt *whileStmt );
42                void premutate( ForStmt *forStmt );
43                Statement * postmutate( ForStmt *forStmt );
44                void premutate( CaseStmt *caseStmt );
45                void premutate( IfStmt *ifStmt );
46                Statement * postmutate( IfStmt *ifStmt );
47                void premutate( SwitchStmt *switchStmt );
48                Statement * postmutate( SwitchStmt *switchStmt );
49                void premutate( TryStmt *tryStmt );
50                Statement * postmutate( TryStmt *tryStmt );
51
52                Statement *mutateLoop( Statement *bodyLoop, Entry &e );
53
54                Label &get_breakLabel() { return breakLabel; }
55                void set_breakLabel( Label newValue ) { breakLabel = newValue; }
56
57                class Entry {
58                  public:
59                        // specialized constructors for each combination of statement with labelled break/continue/fallthrough that is valid to cleanup the use cases
60                        explicit Entry( ForStmt *stmt, Label breakExit, Label contExit ) :
61                                stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {}
62
63                        explicit Entry( WhileStmt *stmt, Label breakExit, Label contExit ) :
64                                stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {}
65
66                        explicit Entry( CompoundStmt *stmt, Label breakExit ) :
67                                stmt( stmt ), breakExit( breakExit ) {}
68
69                        explicit Entry( IfStmt *stmt, Label breakExit ) :
70                                stmt( stmt ), breakExit( breakExit ) {}
71
72                        explicit Entry( CaseStmt *stmt, Label fallExit ) :
73                                stmt( stmt ), fallExit( fallExit ) {}
74
75                        explicit Entry( SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) :
76                                stmt( stmt ), breakExit( breakExit ), fallDefaultExit( fallDefaultExit ) {}
77
78                        explicit Entry( TryStmt *stmt, Label breakExit ) :
79                                stmt( stmt ), breakExit( breakExit ) {}
80
81                        bool operator==( const Statement *other ) { return stmt == other; }
82                        bool operator!=( const Statement *other ) { return stmt != other; }
83
84                        bool operator==( const Entry &other ) { return stmt == other.get_controlStructure(); }
85
86                        Statement *get_controlStructure() const { return stmt; }
87
88                        Label useContExit() { contUsed = true; return contExit; }
89                        Label useBreakExit() { breakUsed = true; return breakExit; }
90                        Label useFallExit() { fallUsed = true; return fallExit; }
91                        Label useFallDefaultExit() { fallDefaultUsed = true; return fallDefaultExit; }
92
93                        bool isContUsed() const { return contUsed; }
94                        bool isBreakUsed() const { return breakUsed; }
95                        bool isFallUsed() const { return fallUsed; }
96                        bool isFallDefaultUsed() const { return fallDefaultUsed; }
97                        void seenDefault() { fallDefaultValid = false; }
98                        bool isFallDefaultValid() const { return fallDefaultValid; }
99                  private:
100                        Statement *stmt;
101                        Label breakExit, contExit, fallExit, fallDefaultExit;
102                        bool breakUsed = false, contUsed = false, fallUsed = false, fallDefaultUsed = false;
103                        bool fallDefaultValid = true;
104                };
105
106          private:
107                std::map< Label, Statement * > *targetTable;
108                std::set< Label > fallthroughLabels;
109                std::list< Entry > enclosingControlStructures;
110                Label breakLabel;
111                LabelGenerator *generator;
112
113                template< typename LoopClass >
114                void prehandleLoopStmt( LoopClass * loopStmt );
115
116                template< typename LoopClass >
117                Statement * posthandleLoopStmt( LoopClass * loopStmt );
118
119                void fixBlock( std::list< Statement * > &kids, bool caseClause = false );
120        };
121} // namespace ControlStruct
122
123// Local Variables: //
124// tab-width: 4 //
125// mode: c++ //
126// compile-command: "make install" //
127// End: //
Note: See TracBrowser for help on using the repository browser.