[51b7345] | 1 | #ifndef LABEL_FIXER_H |
---|
| 2 | #define LABEL_FIXER_H |
---|
| 3 | |
---|
| 4 | #include "utility.h" |
---|
| 5 | |
---|
| 6 | #include "SynTree/SynTree.h" |
---|
| 7 | #include "SynTree/Visitor.h" |
---|
| 8 | |
---|
| 9 | #include "LabelGenerator.h" |
---|
| 10 | |
---|
| 11 | #include <map> |
---|
| 12 | |
---|
| 13 | namespace ControlStruct { |
---|
[d9a0e76] | 14 | class LabelFixer : public Visitor { |
---|
| 15 | typedef Visitor Parent; |
---|
[51b7345] | 16 | public: |
---|
[d9a0e76] | 17 | LabelFixer( LabelGenerator *gen = 0 ); |
---|
| 18 | |
---|
| 19 | std::map < Label, Statement * > *resolveJumps() throw ( SemanticError ); |
---|
| 20 | |
---|
| 21 | // Declarations |
---|
| 22 | virtual void visit( FunctionDecl *functionDecl ); |
---|
| 23 | |
---|
| 24 | // Statements |
---|
| 25 | void visit( Statement *stmt ); |
---|
| 26 | |
---|
| 27 | virtual void visit( CompoundStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 28 | virtual void visit( NullStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 29 | virtual void visit( ExprStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 30 | virtual void visit( IfStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 31 | virtual void visit( WhileStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 32 | virtual void visit( ForStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 33 | virtual void visit( SwitchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 34 | virtual void visit( ChooseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 35 | virtual void visit( FallthruStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 36 | virtual void visit( CaseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 37 | virtual void visit( ReturnStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 38 | virtual void visit( TryStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 39 | virtual void visit( CatchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 40 | virtual void visit( DeclStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
| 41 | virtual void visit( BranchStmt *branchStmt ); |
---|
| 42 | |
---|
| 43 | Label setLabelsDef( std::list< Label > &, Statement *definition ); |
---|
| 44 | Label setLabelsUsg( Label, Statement *usage = 0 ); |
---|
[51b7345] | 45 | |
---|
| 46 | private: |
---|
[d9a0e76] | 47 | class Entry { |
---|
| 48 | public: |
---|
| 49 | Entry( Statement *to = 0, Statement *from = 0 ); |
---|
| 50 | bool used() { return ( usage.empty() ); } |
---|
| 51 | bool defined() { return ( definition != 0 ); } |
---|
| 52 | bool insideLoop(); |
---|
| 53 | |
---|
| 54 | Label get_label() const { return label; } |
---|
| 55 | Statement *get_definition() const { return definition; } |
---|
| 56 | std::list< Statement *> &get_uses() { return usage; } |
---|
| 57 | |
---|
| 58 | void add_use ( Statement *use ) { usage.push_back( use ); } |
---|
| 59 | void add_uses ( std::list<Statement *> uses ) { usage.insert( usage.end(), uses.begin(), uses.end() ); } |
---|
| 60 | void set_definition( Statement *def ) { definition = def; } |
---|
| 61 | |
---|
| 62 | void set_label( Label lab ) { label = lab; } |
---|
| 63 | Label gset_label() const { return label; } |
---|
| 64 | private: |
---|
| 65 | Label label; |
---|
| 66 | Statement *definition; |
---|
| 67 | std::list<Statement *> usage; |
---|
| 68 | }; |
---|
[51b7345] | 69 | |
---|
[d9a0e76] | 70 | std::map < Label, Entry *> labelTable; |
---|
| 71 | LabelGenerator *generator; |
---|
[51b7345] | 72 | }; |
---|
| 73 | } // namespace ControlStruct |
---|
| 74 | |
---|
[d9a0e76] | 75 | #endif // LABEL_FIXER_H |
---|
[51b7345] | 76 | |
---|
| 77 | /* |
---|
| 78 | Local Variables: |
---|
| 79 | mode: c++ |
---|
| 80 | End: |
---|
| 81 | */ |
---|