source: src/ControlStruct/FixLabels.cpp@ cb921d4

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since cb921d4 was cb921d4, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Changed some of the new ast code so they no longer pass around the empty LabelGenerator.

  • Property mode set to 100644
File size: 3.2 KB
Line 
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/LabelGenerator.h"
22#include "ControlStruct/MultiLevelExit.hpp"
23
24namespace ControlStruct {
25
26namespace {
27
28class FixLabelsCore final : public ast::WithGuards {
29 LabelToStmt labelTable;
30public:
31 FixLabelsCore( LabelGenerator * gen = nullptr ) : labelTable() {}
32
33 void previsit( const ast::FunctionDecl * );
34 const ast::FunctionDecl * postvisit( const ast::FunctionDecl * );
35 void previsit( const ast::Stmt * );
36 void previsit( const ast::BranchStmt * );
37 void previsit( const ast::LabelAddressExpr * );
38
39 void setLabelsDef( const std::vector<ast::Label> &, const ast::Stmt * );
40 void setLabelsUsage( const ast::Label & );
41};
42
43void FixLabelsCore::previsit( const ast::FunctionDecl * ) {
44 GuardValue( labelTable ).clear();
45}
46
47const ast::FunctionDecl * FixLabelsCore::postvisit(
48 const ast::FunctionDecl * decl ) {
49 if ( nullptr == decl->stmts ) return decl;
50 for ( auto kvp : labelTable ) {
51 if ( nullptr == kvp.second ) {
52 SemanticError( kvp.first.location,
53 "Use of undefined label: " + kvp.first.name );
54 }
55 }
56 return ast::mutate_field( decl, &ast::FunctionDecl::stmts,
57 multiLevelExitUpdate( decl->stmts.get(), labelTable ) );
58}
59
60void FixLabelsCore::previsit( const ast::Stmt * stmt ) {
61 if ( !stmt->labels.empty() ) {
62 setLabelsDef( stmt->labels, stmt );
63 }
64}
65
66void FixLabelsCore::previsit( const ast::BranchStmt * stmt ) {
67 if ( !stmt->labels.empty() ) {
68 setLabelsDef( stmt->labels, stmt );
69 }
70 if ( !stmt->target.empty() ) {
71 setLabelsUsage( stmt->target );
72 }
73}
74
75void FixLabelsCore::previsit( const ast::LabelAddressExpr * expr ) {
76 assert( !expr->arg.empty() );
77 setLabelsUsage( expr->arg );
78}
79
80void FixLabelsCore::setLabelsDef(
81 const std::vector<ast::Label> & labels, const ast::Stmt * stmt ) {
82 assert( !labels.empty() );
83 assert( stmt );
84
85 for ( auto label : labels ) {
86 if ( labelTable.find( label ) == labelTable.end() ) {
87 // Make sure to only create the entry once.
88 labelTable[ label ] = stmt;
89 } else if ( nullptr != labelTable[ label ] ) {
90 // Duplicate definition, this is an error.
91 SemanticError( label.location,
92 "Duplicate definition of label: " + label.name );
93 } else {
94 // Perviously used, but not defined until now.
95 labelTable[ label ] = stmt;
96 }
97 }
98}
99
100// Label was used, if it is new add it to the table.
101void FixLabelsCore::setLabelsUsage( const ast::Label & label ) {
102 if ( labelTable.find( label ) == labelTable.end() ) {
103 labelTable[ label ] = nullptr;
104 }
105}
106
107} // namespace
108
109void fixLabels( ast::TranslationUnit & translationUnit ) {
110 ast::Pass<FixLabelsCore>::run( translationUnit );
111}
112
113} // namespace ControlStruct
114
115// Local Variables: //
116// tab-width: 4 //
117// mode: c++ //
118// compile-command: "make install" //
119// End: //
Note: See TracBrowser for help on using the repository browser.