| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 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 | // LabelAddressFixer.cpp -- Create label address expressions.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Fri Nov 12 16:30:00 2021
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Fri Nov 12 16:30:00 2021
 | 
|---|
| 13 | // Update Count     : 0
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Validate/LabelAddressFixer.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Decl.hpp"
 | 
|---|
| 19 | #include "AST/Expr.hpp"
 | 
|---|
| 20 | #include "AST/Pass.hpp"
 | 
|---|
| 21 | #include "AST/TranslationUnit.hpp"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include <set>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | namespace Validate {
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace {
 | 
|---|
| 28 | 
 | 
|---|
| 29 | struct LabelFinder {
 | 
|---|
| 30 |         std::set<ast::Label> & labels;
 | 
|---|
| 31 |         LabelFinder( std::set<ast::Label> & labels ) : labels( labels ) {}
 | 
|---|
| 32 |         void previsit( const ast::Stmt * stmt ) {
 | 
|---|
| 33 |                 for ( const ast::Label & label : stmt->labels ) {
 | 
|---|
| 34 |                         labels.insert( label );
 | 
|---|
| 35 |                 }
 | 
|---|
| 36 |         }
 | 
|---|
| 37 | };
 | 
|---|
| 38 | 
 | 
|---|
| 39 | struct LabelAddressFixer : public ast::WithGuards {
 | 
|---|
| 40 |         std::set<ast::Label> labels;
 | 
|---|
| 41 |         void previsit( const ast::FunctionDecl * decl );
 | 
|---|
| 42 |         const ast::Expr * postvisit( const ast::AddressExpr * expr );
 | 
|---|
| 43 | };
 | 
|---|
| 44 | 
 | 
|---|
| 45 | void LabelAddressFixer::previsit( const ast::FunctionDecl * decl ) {
 | 
|---|
| 46 |         GuardValue( labels );
 | 
|---|
| 47 |         ast::Pass<LabelFinder>::read( decl, labels );
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | const ast::Expr * LabelAddressFixer::postvisit( const ast::AddressExpr * expr ) {
 | 
|---|
| 51 |         if ( auto inner = expr->arg.as<ast::AddressExpr>() ) {
 | 
|---|
| 52 |                 if ( auto nameExpr = inner->arg.as<ast::NameExpr>() ) {
 | 
|---|
| 53 |                         ast::Label label( nameExpr->location, nameExpr->name );
 | 
|---|
| 54 |                         if ( labels.count( label ) ) {
 | 
|---|
| 55 |                                 return new ast::LabelAddressExpr( nameExpr->location, std::move( label ) );
 | 
|---|
| 56 |                         }
 | 
|---|
| 57 |                 }
 | 
|---|
| 58 |         }
 | 
|---|
| 59 |         return expr;
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | } // namespace
 | 
|---|
| 63 | 
 | 
|---|
| 64 | void fixLabelAddresses( ast::TranslationUnit & translationUnit ) {
 | 
|---|
| 65 |         ast::Pass<LabelAddressFixer>::run( translationUnit );
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | } // namespace Validate
 | 
|---|
| 69 | 
 | 
|---|
| 70 | // Local Variables: //
 | 
|---|
| 71 | // tab-width: 4 //
 | 
|---|
| 72 | // mode: c++ //
 | 
|---|
| 73 | // compile-command: "make install" //
 | 
|---|
| 74 | // End: //
 | 
|---|