[51587aa] | 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 | // |
---|
[0f8e4ac] | 7 | // MLEMutator.h -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[3b0bc16] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue Feb 1 09:27:24 2022 |
---|
| 13 | // Update Count : 50 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[d180746] | 18 | #include <list> // for list |
---|
| 19 | #include <map> // for map |
---|
| 20 | #include <string> // for string |
---|
[720a007] | 21 | #include <set> // for unordered_set |
---|
[51b7345] | 22 | |
---|
[94e025a2] | 23 | #include "Common/PassVisitor.h" |
---|
[d180746] | 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 |
---|
[51b7345] | 28 | |
---|
| 29 | namespace ControlStruct { |
---|
[94e025a2] | 30 | class LabelGenerator; |
---|
[d180746] | 31 | |
---|
[5cdeecd] | 32 | class MultiLevelExitMutator : public WithVisitorRef<MultiLevelExitMutator>, |
---|
| 33 | public WithShortCircuiting, public WithGuards { |
---|
[d9a0e76] | 34 | public: |
---|
[720a007] | 35 | class Entry; |
---|
[5cdeecd] | 36 | MultiLevelExitMutator( std::map<Label, Statement *> *t, LabelGenerator *gen = 0 ) : |
---|
| 37 | targetTable( t ), breakLabel(std::string("")), generator( gen ) {} |
---|
| 38 | ~MultiLevelExitMutator(); |
---|
[d9a0e76] | 39 | |
---|
[9d6317f] | 40 | void premutate( FunctionDecl * ); |
---|
| 41 | |
---|
[94e025a2] | 42 | void premutate( CompoundStmt *cmpndStmt ); |
---|
[f8965f4] | 43 | Statement * postmutate( BranchStmt *branchStmt ); |
---|
[3b0bc16] | 44 | void premutate( WhileDoStmt *whileDoStmt ); |
---|
| 45 | Statement * postmutate( WhileDoStmt *whileDoStmt ); |
---|
[94e025a2] | 46 | void premutate( ForStmt *forStmt ); |
---|
| 47 | Statement * postmutate( ForStmt *forStmt ); |
---|
| 48 | void premutate( CaseStmt *caseStmt ); |
---|
| 49 | void premutate( IfStmt *ifStmt ); |
---|
| 50 | Statement * postmutate( IfStmt *ifStmt ); |
---|
| 51 | void premutate( SwitchStmt *switchStmt ); |
---|
| 52 | Statement * postmutate( SwitchStmt *switchStmt ); |
---|
[9d6317f] | 53 | void premutate( ReturnStmt *returnStmt ); |
---|
[9bdb8b7] | 54 | void premutate( TryStmt *tryStmt ); |
---|
| 55 | Statement * postmutate( TryStmt *tryStmt ); |
---|
[d62806c] | 56 | void premutate( FinallyStmt *finallyStmt ); |
---|
[d9a0e76] | 57 | |
---|
[a08ba92] | 58 | Statement *mutateLoop( Statement *bodyLoop, Entry &e ); |
---|
[d9a0e76] | 59 | |
---|
[a08ba92] | 60 | Label &get_breakLabel() { return breakLabel; } |
---|
| 61 | void set_breakLabel( Label newValue ) { breakLabel = newValue; } |
---|
[720a007] | 62 | |
---|
[a08ba92] | 63 | class Entry { |
---|
| 64 | public: |
---|
[720a007] | 65 | // specialized constructors for each combination of statement with labelled break/continue/fallthrough that is valid to cleanup the use cases |
---|
| 66 | explicit Entry( ForStmt *stmt, Label breakExit, Label contExit ) : |
---|
| 67 | stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {} |
---|
| 68 | |
---|
[3b0bc16] | 69 | explicit Entry( WhileDoStmt *stmt, Label breakExit, Label contExit ) : |
---|
[720a007] | 70 | stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {} |
---|
[d9a0e76] | 71 | |
---|
[720a007] | 72 | explicit Entry( CompoundStmt *stmt, Label breakExit ) : |
---|
| 73 | stmt( stmt ), breakExit( breakExit ) {} |
---|
[d9a0e76] | 74 | |
---|
[720a007] | 75 | explicit Entry( IfStmt *stmt, Label breakExit ) : |
---|
| 76 | stmt( stmt ), breakExit( breakExit ) {} |
---|
[d9a0e76] | 77 | |
---|
[720a007] | 78 | explicit Entry( CaseStmt *stmt, Label fallExit ) : |
---|
| 79 | stmt( stmt ), fallExit( fallExit ) {} |
---|
| 80 | |
---|
| 81 | explicit Entry( SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) : |
---|
| 82 | stmt( stmt ), breakExit( breakExit ), fallDefaultExit( fallDefaultExit ) {} |
---|
| 83 | |
---|
[9bdb8b7] | 84 | explicit Entry( TryStmt *stmt, Label breakExit ) : |
---|
| 85 | stmt( stmt ), breakExit( breakExit ) {} |
---|
| 86 | |
---|
[720a007] | 87 | bool operator==( const Statement *other ) { return stmt == other; } |
---|
| 88 | bool operator!=( const Statement *other ) { return stmt != other; } |
---|
| 89 | |
---|
| 90 | bool operator==( const Entry &other ) { return stmt == other.get_controlStructure(); } |
---|
| 91 | |
---|
| 92 | Statement *get_controlStructure() const { return stmt; } |
---|
[51b7345] | 93 | |
---|
[27de955] | 94 | Label useContExit() { contUsed = true; return contExit; } |
---|
| 95 | Label useBreakExit() { breakUsed = true; return breakExit; } |
---|
[720a007] | 96 | Label useFallExit() { fallUsed = true; return fallExit; } |
---|
| 97 | Label useFallDefaultExit() { fallDefaultUsed = true; return fallDefaultExit; } |
---|
[a08ba92] | 98 | |
---|
[27de955] | 99 | bool isContUsed() const { return contUsed; } |
---|
| 100 | bool isBreakUsed() const { return breakUsed; } |
---|
[720a007] | 101 | bool isFallUsed() const { return fallUsed; } |
---|
| 102 | bool isFallDefaultUsed() const { return fallDefaultUsed; } |
---|
| 103 | void seenDefault() { fallDefaultValid = false; } |
---|
| 104 | bool isFallDefaultValid() const { return fallDefaultValid; } |
---|
[a08ba92] | 105 | private: |
---|
[720a007] | 106 | Statement *stmt; |
---|
| 107 | Label breakExit, contExit, fallExit, fallDefaultExit; |
---|
| 108 | bool breakUsed = false, contUsed = false, fallUsed = false, fallDefaultUsed = false; |
---|
| 109 | bool fallDefaultValid = true; |
---|
[a08ba92] | 110 | }; |
---|
| 111 | |
---|
[720a007] | 112 | private: |
---|
[a08ba92] | 113 | std::map< Label, Statement * > *targetTable; |
---|
[720a007] | 114 | std::set< Label > fallthroughLabels; |
---|
[e39aa0f] | 115 | std::list< Entry > enclosingControlStructures; |
---|
[a08ba92] | 116 | Label breakLabel; |
---|
| 117 | LabelGenerator *generator; |
---|
[9d6317f] | 118 | bool inFinally = false; |
---|
[be5aa1b] | 119 | |
---|
[27de955] | 120 | template< typename LoopClass > |
---|
[94e025a2] | 121 | void prehandleLoopStmt( LoopClass * loopStmt ); |
---|
[27de955] | 122 | |
---|
[94e025a2] | 123 | template< typename LoopClass > |
---|
| 124 | Statement * posthandleLoopStmt( LoopClass * loopStmt ); |
---|
[27de955] | 125 | |
---|
[720a007] | 126 | void fixBlock( std::list< Statement * > &kids, bool caseClause = false ); |
---|
[a08ba92] | 127 | }; |
---|
[51b7345] | 128 | } // namespace ControlStruct |
---|
| 129 | |
---|
[51587aa] | 130 | // Local Variables: // |
---|
| 131 | // tab-width: 4 // |
---|
| 132 | // mode: c++ // |
---|
| 133 | // compile-command: "make install" // |
---|
| 134 | // End: // |
---|