| 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 Feb  1 09:27:24 2022 | 
|---|
| 13 | // Update Count     : 50 | 
|---|
| 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 |  | 
|---|
| 29 | namespace ControlStruct { | 
|---|
| 30 | class LabelGenerator; | 
|---|
| 31 |  | 
|---|
| 32 | class MultiLevelExitMutator : public WithVisitorRef<MultiLevelExitMutator>, | 
|---|
| 33 | public WithShortCircuiting, public WithGuards { | 
|---|
| 34 | public: | 
|---|
| 35 | class Entry; | 
|---|
| 36 | MultiLevelExitMutator( std::map<Label, Statement *> *t, LabelGenerator *gen = 0 ) : | 
|---|
| 37 | targetTable( t ), breakLabel(std::string("")), generator( gen ) {} | 
|---|
| 38 | ~MultiLevelExitMutator(); | 
|---|
| 39 |  | 
|---|
| 40 | void premutate( FunctionDecl * ); | 
|---|
| 41 |  | 
|---|
| 42 | void premutate( CompoundStmt *cmpndStmt ); | 
|---|
| 43 | Statement * postmutate( BranchStmt *branchStmt ); | 
|---|
| 44 | void premutate( WhileDoStmt *whileDoStmt ); | 
|---|
| 45 | Statement * postmutate( WhileDoStmt *whileDoStmt ); | 
|---|
| 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 ); | 
|---|
| 53 | void premutate( ReturnStmt *returnStmt ); | 
|---|
| 54 | void premutate( TryStmt *tryStmt ); | 
|---|
| 55 | Statement * postmutate( TryStmt *tryStmt ); | 
|---|
| 56 | void premutate( FinallyStmt *finallyStmt ); | 
|---|
| 57 |  | 
|---|
| 58 | Statement *mutateLoop( Statement *bodyLoop, Entry &e ); | 
|---|
| 59 |  | 
|---|
| 60 | Label &get_breakLabel() { return breakLabel; } | 
|---|
| 61 | void set_breakLabel( Label newValue ) { breakLabel = newValue; } | 
|---|
| 62 |  | 
|---|
| 63 | class Entry { | 
|---|
| 64 | public: | 
|---|
| 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 |  | 
|---|
| 69 | explicit Entry( WhileDoStmt *stmt, Label breakExit, Label contExit ) : | 
|---|
| 70 | stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {} | 
|---|
| 71 |  | 
|---|
| 72 | explicit Entry( CompoundStmt *stmt, Label breakExit ) : | 
|---|
| 73 | stmt( stmt ), breakExit( breakExit ) {} | 
|---|
| 74 |  | 
|---|
| 75 | explicit Entry( IfStmt *stmt, Label breakExit ) : | 
|---|
| 76 | stmt( stmt ), breakExit( breakExit ) {} | 
|---|
| 77 |  | 
|---|
| 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 |  | 
|---|
| 84 | explicit Entry( TryStmt *stmt, Label breakExit ) : | 
|---|
| 85 | stmt( stmt ), breakExit( breakExit ) {} | 
|---|
| 86 |  | 
|---|
| 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; } | 
|---|
| 93 |  | 
|---|
| 94 | Label useContExit() { contUsed = true; return contExit; } | 
|---|
| 95 | Label useBreakExit() { breakUsed = true; return breakExit; } | 
|---|
| 96 | Label useFallExit() { fallUsed = true; return fallExit; } | 
|---|
| 97 | Label useFallDefaultExit() { fallDefaultUsed = true; return fallDefaultExit; } | 
|---|
| 98 |  | 
|---|
| 99 | bool isContUsed() const { return contUsed; } | 
|---|
| 100 | bool isBreakUsed() const { return breakUsed; } | 
|---|
| 101 | bool isFallUsed() const { return fallUsed; } | 
|---|
| 102 | bool isFallDefaultUsed() const { return fallDefaultUsed; } | 
|---|
| 103 | void seenDefault() { fallDefaultValid = false; } | 
|---|
| 104 | bool isFallDefaultValid() const { return fallDefaultValid; } | 
|---|
| 105 | private: | 
|---|
| 106 | Statement *stmt; | 
|---|
| 107 | Label breakExit, contExit, fallExit, fallDefaultExit; | 
|---|
| 108 | bool breakUsed = false, contUsed = false, fallUsed = false, fallDefaultUsed = false; | 
|---|
| 109 | bool fallDefaultValid = true; | 
|---|
| 110 | }; | 
|---|
| 111 |  | 
|---|
| 112 | private: | 
|---|
| 113 | std::map< Label, Statement * > *targetTable; | 
|---|
| 114 | std::set< Label > fallthroughLabels; | 
|---|
| 115 | std::list< Entry > enclosingControlStructures; | 
|---|
| 116 | Label breakLabel; | 
|---|
| 117 | LabelGenerator *generator; | 
|---|
| 118 | bool inFinally = false; | 
|---|
| 119 |  | 
|---|
| 120 | template< typename LoopClass > | 
|---|
| 121 | void prehandleLoopStmt( LoopClass * loopStmt ); | 
|---|
| 122 |  | 
|---|
| 123 | template< typename LoopClass > | 
|---|
| 124 | Statement * posthandleLoopStmt( LoopClass * loopStmt ); | 
|---|
| 125 |  | 
|---|
| 126 | void fixBlock( std::list< Statement * > &kids, bool caseClause = false ); | 
|---|
| 127 | }; | 
|---|
| 128 | } // namespace ControlStruct | 
|---|
| 129 |  | 
|---|
| 130 | // Local Variables: // | 
|---|
| 131 | // tab-width: 4 // | 
|---|
| 132 | // mode: c++ // | 
|---|
| 133 | // compile-command: "make install" // | 
|---|
| 134 | // End: // | 
|---|