source: src/Validate/EliminateTypedef.cpp

Last change on this file was 1931bb01, checked in by Andrew Beach <ajbeach@…>, 22 months ago

Converted 'Validate A' to the new AST. There some utility changes as well.

  • Property mode set to 100644
File size: 3.0 KB
Line 
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 Jul 11 16:30:00 2022
13// Update Count     : 1
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
25namespace Validate {
26
27namespace {
28
29struct EliminateTypedefCore {
30        // Remove typedefs from inside aggregates.
31        ast::StructDecl const * previsit( ast::StructDecl const * decl );
32        ast::UnionDecl const * previsit( ast::UnionDecl const * decl );
33        // Remove typedefs from statement lists.
34        ast::CompoundStmt const * previsit( ast::CompoundStmt const * stmt );
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 );
39};
40
41bool isTypedef( ast::ptr<ast::Decl> const & decl ) {
42        return (nullptr != decl.as<ast::TypedefDecl>());
43}
44
45bool 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
52template<typename node_t, typename super_t, typename field_t, typename pred_t>
53node_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
59ast::StructDecl const * EliminateTypedefCore::previsit( ast::StructDecl const * decl ) {
60        return field_erase_if( decl, &ast::StructDecl::members, isTypedef );
61}
62
63ast::UnionDecl const * EliminateTypedefCore::previsit( ast::UnionDecl const * decl ) {
64        return field_erase_if( decl, &ast::UnionDecl::members, isTypedef );
65}
66
67ast::CompoundStmt const * EliminateTypedefCore::previsit( ast::CompoundStmt const * stmt ) {
68        return field_erase_if( stmt, &ast::CompoundStmt::kids, isTypedefStmt );
69}
70
71ast::IfStmt const * EliminateTypedefCore::previsit( ast::IfStmt const * stmt ) {
72        return field_erase_if( stmt, &ast::IfStmt::inits, isTypedefStmt );
73}
74
75ast::ForStmt const * EliminateTypedefCore::previsit( ast::ForStmt const * stmt ) {
76        return field_erase_if( stmt, &ast::ForStmt::inits, isTypedefStmt );
77}
78
79ast::WhileDoStmt const * EliminateTypedefCore::previsit( ast::WhileDoStmt const * stmt ) {
80        return field_erase_if( stmt, &ast::WhileDoStmt::inits, isTypedefStmt );
81}
82
83} // namespace
84
85/// Removes TypedefDecl nodes from the AST.
86void 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: //
Note: See TracBrowser for help on using the repository browser.