| 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
|
|---|
| 12 | // Last Modified On : Mon Apr 25 14:26:00 2022
|
|---|
| 13 | // Update Count : 0
|
|---|
| 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 {
|
|---|
| 30 | ast::StructDecl const * previsit( ast::StructDecl const * decl );
|
|---|
| 31 | ast::UnionDecl const * previsit( ast::UnionDecl const * decl );
|
|---|
| 32 | ast::CompoundStmt const * previsit( ast::CompoundStmt const * stmt );
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | bool isTypedef( ast::ptr<ast::Decl> const & decl ) {
|
|---|
| 36 | return (nullptr != decl.as<ast::TypedefDecl>());
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | bool isTypedefStmt( ast::ptr<ast::Stmt> const & stmt ) {
|
|---|
| 40 | if ( auto declStmt = stmt.as<ast::DeclStmt>() ) {
|
|---|
| 41 | return isTypedef( declStmt->decl );
|
|---|
| 42 | }
|
|---|
| 43 | return false;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | template<typename node_t, typename super_t, typename field_t, typename pred_t>
|
|---|
| 47 | node_t const * field_erase_if( node_t const * node, field_t super_t::*field, pred_t && pred) {
|
|---|
| 48 | node_t * mut = ast::mutate( node );
|
|---|
| 49 | erase_if( mut->*field, pred );
|
|---|
| 50 | return mut;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | ast::StructDecl const * EliminateTypedefCore::previsit( ast::StructDecl const * decl ) {
|
|---|
| 54 | return field_erase_if( decl, &ast::StructDecl::members, isTypedef );
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | ast::UnionDecl const * EliminateTypedefCore::previsit( ast::UnionDecl const * decl ) {
|
|---|
| 58 | return field_erase_if( decl, &ast::UnionDecl::members, isTypedef );
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | ast::CompoundStmt const * EliminateTypedefCore::previsit( ast::CompoundStmt const * stmt ) {
|
|---|
| 62 | return field_erase_if( stmt, &ast::CompoundStmt::kids, isTypedefStmt );
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | } // namespace
|
|---|
| 66 |
|
|---|
| 67 | /// Removes TypedefDecl nodes from the AST.
|
|---|
| 68 | void eliminateTypedef( ast::TranslationUnit & translationUnit ) {
|
|---|
| 69 | ast::Pass<EliminateTypedefCore>::run( translationUnit );
|
|---|
| 70 | erase_if( translationUnit.decls, isTypedef );
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | } // namespace Validate
|
|---|
| 74 |
|
|---|
| 75 | // Local Variables: //
|
|---|
| 76 | // tab-width: 4 //
|
|---|
| 77 | // mode: c++ //
|
|---|
| 78 | // compile-command: "make install" //
|
|---|
| 79 | // End: //
|
|---|