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 | // LabelFixer.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 : Sat Jul 22 09:17:24 2017 |
---|
13 | // Update Count : 34 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "Common/utility.h" |
---|
19 | #include "SynTree/SynTree.h" |
---|
20 | #include "SynTree/Visitor.h" |
---|
21 | #include "SynTree/Label.h" |
---|
22 | #include "LabelGenerator.h" |
---|
23 | #include <map> |
---|
24 | |
---|
25 | namespace ControlStruct { |
---|
26 | /// normalizes label definitions and generates multi-level exit labels |
---|
27 | class LabelFixer final : public Visitor { |
---|
28 | typedef Visitor Parent; |
---|
29 | public: |
---|
30 | LabelFixer( LabelGenerator *gen = 0 ); |
---|
31 | |
---|
32 | std::map < Label, Statement * > *resolveJumps() throw ( SemanticError ); |
---|
33 | |
---|
34 | using Visitor::visit; |
---|
35 | |
---|
36 | // Declarations |
---|
37 | virtual void visit( FunctionDecl *functionDecl ) override; |
---|
38 | |
---|
39 | // Statements |
---|
40 | void visit( Statement *stmt ); |
---|
41 | |
---|
42 | virtual void visit( CompoundStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
43 | virtual void visit( NullStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
44 | virtual void visit( ExprStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
45 | virtual void visit( IfStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
46 | virtual void visit( WhileStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
47 | virtual void visit( ForStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
48 | virtual void visit( SwitchStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
49 | virtual void visit( CaseStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
50 | virtual void visit( ReturnStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
51 | virtual void visit( TryStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
52 | virtual void visit( CatchStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
53 | virtual void visit( DeclStmt *stmt ) override { visit( (Statement *)stmt ); return Parent::visit( stmt ); } |
---|
54 | virtual void visit( BranchStmt *branchStmt ) override; |
---|
55 | virtual void visit( UntypedExpr *untyped ) override; |
---|
56 | |
---|
57 | Label setLabelsDef( std::list< Label > &, Statement *definition ); |
---|
58 | template< typename UsageNode > |
---|
59 | void setLabelsUsg( Label, UsageNode *usage = 0 ); |
---|
60 | |
---|
61 | private: |
---|
62 | class Entry { |
---|
63 | public: |
---|
64 | Entry( Statement *to ) : definition( to ) {} |
---|
65 | bool defined() { return ( definition != 0 ); } |
---|
66 | bool insideLoop(); |
---|
67 | |
---|
68 | Label get_label() const { return label; } |
---|
69 | void set_label( Label lab ) { label = lab; } |
---|
70 | |
---|
71 | Statement *get_definition() const { return definition; } |
---|
72 | void set_definition( Statement *def ) { definition = def; } |
---|
73 | |
---|
74 | private: |
---|
75 | Label label; |
---|
76 | Statement *definition; |
---|
77 | }; |
---|
78 | |
---|
79 | std::map < Label, Entry *> labelTable; |
---|
80 | LabelGenerator *generator; |
---|
81 | }; |
---|
82 | } // namespace ControlStruct |
---|
83 | |
---|
84 | // Local Variables: // |
---|
85 | // tab-width: 4 // |
---|
86 | // mode: c++ // |
---|
87 | // compile-command: "make install" // |
---|
88 | // End: // |
---|