[298fe57] | 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 | // EliminateTypedef.cpp -- Removes TypedefDecl nodes from the AST. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Wed Apr 20 16:37:00 2022 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
[1931bb01] | 12 | // Last Modified On : Mon Jul 11 16:30:00 2022 |
---|
| 13 | // Update Count : 1 |
---|
[298fe57] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Validate/EliminateTypedef.hpp" |
---|
| 17 | |
---|
| 18 | #include <algorithm> |
---|
| 19 | |
---|
| 20 | #include "AST/Decl.hpp" |
---|
| 21 | #include "AST/Pass.hpp" |
---|
| 22 | #include "AST/Stmt.hpp" |
---|
| 23 | #include "Common/utility.h" |
---|
| 24 | |
---|
| 25 | namespace Validate { |
---|
| 26 | |
---|
| 27 | namespace { |
---|
| 28 | |
---|
| 29 | struct EliminateTypedefCore { |
---|
[1931bb01] | 30 | // Remove typedefs from inside aggregates. |
---|
[298fe57] | 31 | ast::StructDecl const * previsit( ast::StructDecl const * decl ); |
---|
| 32 | ast::UnionDecl const * previsit( ast::UnionDecl const * decl ); |
---|
[1931bb01] | 33 | // Remove typedefs from statement lists. |
---|
[298fe57] | 34 | ast::CompoundStmt const * previsit( ast::CompoundStmt const * stmt ); |
---|
[1931bb01] | 35 | // Remove typedefs from control structure initializers. |
---|
| 36 | ast::IfStmt const * previsit( ast::IfStmt const * stmt ); |
---|
| 37 | ast::ForStmt const * previsit( ast::ForStmt const * stmt ); |
---|
| 38 | ast::WhileDoStmt const * previsit( ast::WhileDoStmt const * stmt ); |
---|
[298fe57] | 39 | }; |
---|
| 40 | |
---|
| 41 | bool isTypedef( ast::ptr<ast::Decl> const & decl ) { |
---|
| 42 | return (nullptr != decl.as<ast::TypedefDecl>()); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | bool isTypedefStmt( ast::ptr<ast::Stmt> const & stmt ) { |
---|
| 46 | if ( auto declStmt = stmt.as<ast::DeclStmt>() ) { |
---|
| 47 | return isTypedef( declStmt->decl ); |
---|
| 48 | } |
---|
| 49 | return false; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | template<typename node_t, typename super_t, typename field_t, typename pred_t> |
---|
| 53 | node_t const * field_erase_if( node_t const * node, field_t super_t::*field, pred_t && pred) { |
---|
| 54 | node_t * mut = ast::mutate( node ); |
---|
| 55 | erase_if( mut->*field, pred ); |
---|
| 56 | return mut; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | ast::StructDecl const * EliminateTypedefCore::previsit( ast::StructDecl const * decl ) { |
---|
| 60 | return field_erase_if( decl, &ast::StructDecl::members, isTypedef ); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | ast::UnionDecl const * EliminateTypedefCore::previsit( ast::UnionDecl const * decl ) { |
---|
| 64 | return field_erase_if( decl, &ast::UnionDecl::members, isTypedef ); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | ast::CompoundStmt const * EliminateTypedefCore::previsit( ast::CompoundStmt const * stmt ) { |
---|
| 68 | return field_erase_if( stmt, &ast::CompoundStmt::kids, isTypedefStmt ); |
---|
| 69 | } |
---|
| 70 | |
---|
[1931bb01] | 71 | ast::IfStmt const * EliminateTypedefCore::previsit( ast::IfStmt const * stmt ) { |
---|
| 72 | return field_erase_if( stmt, &ast::IfStmt::inits, isTypedefStmt ); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | ast::ForStmt const * EliminateTypedefCore::previsit( ast::ForStmt const * stmt ) { |
---|
| 76 | return field_erase_if( stmt, &ast::ForStmt::inits, isTypedefStmt ); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | ast::WhileDoStmt const * EliminateTypedefCore::previsit( ast::WhileDoStmt const * stmt ) { |
---|
| 80 | return field_erase_if( stmt, &ast::WhileDoStmt::inits, isTypedefStmt ); |
---|
| 81 | } |
---|
| 82 | |
---|
[298fe57] | 83 | } // namespace |
---|
| 84 | |
---|
| 85 | /// Removes TypedefDecl nodes from the AST. |
---|
| 86 | void eliminateTypedef( ast::TranslationUnit & translationUnit ) { |
---|
| 87 | ast::Pass<EliminateTypedefCore>::run( translationUnit ); |
---|
| 88 | erase_if( translationUnit.decls, isTypedef ); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | } // namespace Validate |
---|
| 92 | |
---|
| 93 | // Local Variables: // |
---|
| 94 | // tab-width: 4 // |
---|
| 95 | // mode: c++ // |
---|
| 96 | // compile-command: "make install" // |
---|
| 97 | // End: // |
---|