| [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
 | 
|---|
| [adcc065] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [720a007] | 12 | // Last Modified On : Thu Mar  8 16:42:32 2018
 | 
|---|
 | 13 | // Update Count     : 41
 | 
|---|
| [51587aa] | 14 | //
 | 
|---|
| [a08ba92] | 15 | 
 | 
|---|
| [6b0b624] | 16 | #pragma once
 | 
|---|
| [51b73452] | 17 | 
 | 
|---|
| [d180746] | 18 | #include <list>                    // for list
 | 
|---|
 | 19 | #include <map>                     // for map
 | 
|---|
 | 20 | #include <string>                  // for string
 | 
|---|
| [720a007] | 21 | #include <set>                     // for unordered_set
 | 
|---|
| [51b73452] | 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
 | 
|---|
| [51b73452] | 28 | 
 | 
|---|
 | 29 | namespace ControlStruct {
 | 
|---|
| [94e025a2] | 30 |         class LabelGenerator;
 | 
|---|
| [d180746] | 31 | 
 | 
|---|
| [720a007] | 32 |         class MLEMutator : public WithVisitorRef<MLEMutator>, public WithShortCircuiting, public WithGuards {
 | 
|---|
| [d9a0e76] | 33 |           public:
 | 
|---|
| [720a007] | 34 |                 class Entry;
 | 
|---|
| [a08ba92] | 35 |                 MLEMutator( std::map<Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {}
 | 
|---|
 | 36 |                 ~MLEMutator();
 | 
|---|
| [d9a0e76] | 37 | 
 | 
|---|
| [94e025a2] | 38 |                 void premutate( CompoundStmt *cmpndStmt );
 | 
|---|
| [a16764a6] | 39 |                 Statement * postmutate( BranchStmt *branchStmt ) throw ( SemanticErrorException );
 | 
|---|
| [94e025a2] | 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 );
 | 
|---|
| [d9a0e76] | 49 | 
 | 
|---|
| [a08ba92] | 50 |                 Statement *mutateLoop( Statement *bodyLoop, Entry &e );
 | 
|---|
| [d9a0e76] | 51 | 
 | 
|---|
| [a08ba92] | 52 |                 Label &get_breakLabel() { return breakLabel; }
 | 
|---|
 | 53 |                 void set_breakLabel( Label newValue ) { breakLabel = newValue; }
 | 
|---|
| [720a007] | 54 | 
 | 
|---|
| [a08ba92] | 55 |                 class Entry {
 | 
|---|
 | 56 |                   public:
 | 
|---|
| [720a007] | 57 |                         // specialized constructors for each combination of statement with labelled break/continue/fallthrough that is valid to cleanup the use cases
 | 
|---|
 | 58 |                         explicit Entry( ForStmt *stmt, Label breakExit, Label contExit ) :
 | 
|---|
 | 59 |                                 stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {}
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |                         explicit Entry( WhileStmt *stmt, Label breakExit, Label contExit ) :
 | 
|---|
 | 62 |                                 stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {}
 | 
|---|
| [d9a0e76] | 63 | 
 | 
|---|
| [720a007] | 64 |                         explicit Entry( CompoundStmt *stmt, Label breakExit ) :
 | 
|---|
 | 65 |                                 stmt( stmt ), breakExit( breakExit ) {}
 | 
|---|
| [d9a0e76] | 66 | 
 | 
|---|
| [720a007] | 67 |                         explicit Entry( IfStmt *stmt, Label breakExit ) :
 | 
|---|
 | 68 |                                 stmt( stmt ), breakExit( breakExit ) {}
 | 
|---|
| [d9a0e76] | 69 | 
 | 
|---|
| [720a007] | 70 |                         explicit Entry( CaseStmt *stmt, Label fallExit ) :
 | 
|---|
 | 71 |                                 stmt( stmt ), fallExit( fallExit ) {}
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |                         explicit Entry( SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) :
 | 
|---|
 | 74 |                                 stmt( stmt ), breakExit( breakExit ), fallDefaultExit( fallDefaultExit ) {}
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |                         bool operator==( const Statement *other ) { return stmt == other; }
 | 
|---|
 | 77 |                         bool operator!=( const Statement *other ) { return stmt != other; }
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 |                         bool operator==( const Entry &other ) { return stmt == other.get_controlStructure(); }
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |                         Statement *get_controlStructure() const { return stmt; }
 | 
|---|
| [51b73452] | 82 | 
 | 
|---|
| [27de955] | 83 |                         Label useContExit() { contUsed = true; return contExit; }
 | 
|---|
 | 84 |                         Label useBreakExit() { breakUsed = true; return breakExit; }
 | 
|---|
| [720a007] | 85 |                         Label useFallExit() { fallUsed = true; return fallExit; }
 | 
|---|
 | 86 |                         Label useFallDefaultExit() { fallDefaultUsed = true; return fallDefaultExit; }
 | 
|---|
| [a08ba92] | 87 | 
 | 
|---|
| [27de955] | 88 |                         bool isContUsed() const { return contUsed; }
 | 
|---|
 | 89 |                         bool isBreakUsed() const { return breakUsed; }
 | 
|---|
| [720a007] | 90 |                         bool isFallUsed() const { return fallUsed; }
 | 
|---|
 | 91 |                         bool isFallDefaultUsed() const { return fallDefaultUsed; }
 | 
|---|
 | 92 |                         void seenDefault() { fallDefaultValid = false; }
 | 
|---|
 | 93 |                         bool isFallDefaultValid() const { return fallDefaultValid; }
 | 
|---|
| [a08ba92] | 94 |                   private:
 | 
|---|
| [720a007] | 95 |                         Statement *stmt;
 | 
|---|
 | 96 |                         Label breakExit, contExit, fallExit, fallDefaultExit;
 | 
|---|
 | 97 |                         bool breakUsed = false, contUsed = false, fallUsed = false, fallDefaultUsed = false;
 | 
|---|
 | 98 |                         bool fallDefaultValid = true;
 | 
|---|
| [a08ba92] | 99 |                 };
 | 
|---|
 | 100 | 
 | 
|---|
| [720a007] | 101 |           private:
 | 
|---|
| [a08ba92] | 102 |                 std::map< Label, Statement * > *targetTable;
 | 
|---|
| [720a007] | 103 |                 std::set< Label > fallthroughLabels;
 | 
|---|
| [e39aa0f] | 104 |                 std::list< Entry > enclosingControlStructures;
 | 
|---|
| [a08ba92] | 105 |                 Label breakLabel;
 | 
|---|
 | 106 |                 LabelGenerator *generator;
 | 
|---|
| [be5aa1b] | 107 | 
 | 
|---|
| [27de955] | 108 |                 template< typename LoopClass >
 | 
|---|
| [94e025a2] | 109 |                 void prehandleLoopStmt( LoopClass * loopStmt );
 | 
|---|
| [27de955] | 110 | 
 | 
|---|
| [94e025a2] | 111 |                 template< typename LoopClass >
 | 
|---|
 | 112 |                 Statement * posthandleLoopStmt( LoopClass * loopStmt );
 | 
|---|
| [27de955] | 113 | 
 | 
|---|
| [720a007] | 114 |                 void fixBlock( std::list< Statement * > &kids, bool caseClause = false );
 | 
|---|
| [a08ba92] | 115 |         };
 | 
|---|
| [51b73452] | 116 | } // namespace ControlStruct
 | 
|---|
 | 117 | 
 | 
|---|
| [51587aa] | 118 | // Local Variables: //
 | 
|---|
 | 119 | // tab-width: 4 //
 | 
|---|
 | 120 | // mode: c++ //
 | 
|---|
 | 121 | // compile-command: "make install" //
 | 
|---|
 | 122 | // End: //
 | 
|---|