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 | // FixLabels.cpp -- Normalizes labels and handles multi-level exit labels. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Mon Nov 1 09:39:00 2021 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Mon Nov 8 10:53:00 2021 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include "FixLabels.hpp" |
---|
17 | |
---|
18 | #include "AST/Label.hpp" |
---|
19 | #include "AST/Pass.hpp" |
---|
20 | #include "AST/Stmt.hpp" |
---|
21 | #include "ControlStruct/MultiLevelExit.hpp" |
---|
22 | |
---|
23 | namespace ControlStruct { |
---|
24 | |
---|
25 | namespace { |
---|
26 | |
---|
27 | class FixLabelsCore final : public ast::WithGuards { |
---|
28 | LabelToStmt labelTable; |
---|
29 | public: |
---|
30 | FixLabelsCore() : labelTable() {} |
---|
31 | |
---|
32 | void previsit( const ast::FunctionDecl * ); |
---|
33 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * ); |
---|
34 | void previsit( const ast::Stmt * ); |
---|
35 | void previsit( const ast::BranchStmt * ); |
---|
36 | void previsit( const ast::LabelAddressExpr * ); |
---|
37 | |
---|
38 | void setLabelsDef( const std::vector<ast::Label> &, const ast::Stmt * ); |
---|
39 | void setLabelsUsage( const ast::Label & ); |
---|
40 | }; |
---|
41 | |
---|
42 | void FixLabelsCore::previsit( const ast::FunctionDecl * ) { |
---|
43 | GuardValue( labelTable ).clear(); |
---|
44 | } |
---|
45 | |
---|
46 | const ast::FunctionDecl * FixLabelsCore::postvisit( |
---|
47 | const ast::FunctionDecl * decl ) { |
---|
48 | if ( nullptr == decl->stmts ) return decl; |
---|
49 | for ( auto kvp : labelTable ) { |
---|
50 | if ( nullptr == kvp.second ) { |
---|
51 | SemanticError( kvp.first.location, |
---|
52 | "Use of undefined label: " + kvp.first.name ); |
---|
53 | } |
---|
54 | } |
---|
55 | return ast::mutate_field( decl, &ast::FunctionDecl::stmts, |
---|
56 | multiLevelExitUpdate( decl->stmts.get(), labelTable ) ); |
---|
57 | } |
---|
58 | |
---|
59 | void FixLabelsCore::previsit( const ast::Stmt * stmt ) { |
---|
60 | if ( !stmt->labels.empty() ) { |
---|
61 | setLabelsDef( stmt->labels, stmt ); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | void FixLabelsCore::previsit( const ast::BranchStmt * stmt ) { |
---|
66 | if ( !stmt->labels.empty() ) { |
---|
67 | setLabelsDef( stmt->labels, stmt ); |
---|
68 | } |
---|
69 | if ( !stmt->target.empty() ) { |
---|
70 | setLabelsUsage( stmt->target ); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | void FixLabelsCore::previsit( const ast::LabelAddressExpr * expr ) { |
---|
75 | assert( !expr->arg.empty() ); |
---|
76 | setLabelsUsage( expr->arg ); |
---|
77 | } |
---|
78 | |
---|
79 | void FixLabelsCore::setLabelsDef( |
---|
80 | const std::vector<ast::Label> & labels, const ast::Stmt * stmt ) { |
---|
81 | assert( !labels.empty() ); |
---|
82 | assert( stmt ); |
---|
83 | |
---|
84 | for ( auto label : labels ) { |
---|
85 | if ( labelTable.find( label ) == labelTable.end() ) { |
---|
86 | // Make sure to only create the entry once. |
---|
87 | labelTable[ label ] = stmt; |
---|
88 | } else if ( nullptr != labelTable[ label ] ) { |
---|
89 | // Duplicate definition, this is an error. |
---|
90 | SemanticError( label.location, |
---|
91 | "Duplicate definition of label: " + label.name ); |
---|
92 | } else { |
---|
93 | // Perviously used, but not defined until now. |
---|
94 | labelTable[ label ] = stmt; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | // Label was used, if it is new add it to the table. |
---|
100 | void FixLabelsCore::setLabelsUsage( const ast::Label & label ) { |
---|
101 | if ( labelTable.find( label ) == labelTable.end() ) { |
---|
102 | labelTable[ label ] = nullptr; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | } // namespace |
---|
107 | |
---|
108 | void fixLabels( ast::TranslationUnit & translationUnit ) { |
---|
109 | ast::Pass<FixLabelsCore>::run( translationUnit ); |
---|
110 | } |
---|
111 | |
---|
112 | } // namespace ControlStruct |
---|
113 | |
---|
114 | // Local Variables: // |
---|
115 | // tab-width: 4 // |
---|
116 | // mode: c++ // |
---|
117 | // compile-command: "make install" // |
---|
118 | // End: // |
---|