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