[f69fac7] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2019 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 | // Util.hpp -- General utilities for working with the AST. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Wed Jan 19 9:46:00 2022 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
[33b7d49] | 12 | // Last Modified On : Fri Mar 11 18:07:00 2022 |
---|
| 13 | // Update Count : 1 |
---|
[f69fac7] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Util.hpp" |
---|
| 17 | |
---|
| 18 | #include "Node.hpp" |
---|
[33b7d49] | 19 | #include "ParseNode.hpp" |
---|
[f69fac7] | 20 | #include "Pass.hpp" |
---|
| 21 | #include "TranslationUnit.hpp" |
---|
| 22 | |
---|
| 23 | #include <vector> |
---|
| 24 | |
---|
| 25 | namespace ast { |
---|
| 26 | |
---|
| 27 | namespace { |
---|
| 28 | |
---|
| 29 | /// Check that ast::ptr/strong references do not form a cycle. |
---|
| 30 | struct NoStrongCyclesCore { |
---|
| 31 | std::vector<const Node *> parents; |
---|
| 32 | |
---|
| 33 | void previsit( const Node * node ) { |
---|
| 34 | for ( auto & parent : parents ) { |
---|
| 35 | assert( parent != node ); |
---|
| 36 | } |
---|
| 37 | parents.push_back( node ); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void postvisit( const Node * node ) { |
---|
| 41 | assert( !parents.empty() ); |
---|
| 42 | assert( parents.back() == node ); |
---|
| 43 | parents.pop_back(); |
---|
| 44 | } |
---|
| 45 | }; |
---|
| 46 | |
---|
[33b7d49] | 47 | /// Check that every note that can has a set CodeLocation. |
---|
| 48 | struct SetCodeLocationsCore { |
---|
| 49 | void previsit( const ParseNode * node ) { |
---|
| 50 | assert( node->location.isSet() ); |
---|
| 51 | } |
---|
| 52 | }; |
---|
| 53 | |
---|
[f69fac7] | 54 | struct InvariantCore { |
---|
| 55 | // To save on the number of visits: this is a kind of composed core. |
---|
| 56 | // None of the passes should make changes so ordering doesn't matter. |
---|
| 57 | NoStrongCyclesCore no_strong_cycles; |
---|
[33b7d49] | 58 | SetCodeLocationsCore set_code_locations; |
---|
[f69fac7] | 59 | |
---|
| 60 | void previsit( const Node * node ) { |
---|
| 61 | no_strong_cycles.previsit( node ); |
---|
| 62 | } |
---|
| 63 | |
---|
[33b7d49] | 64 | void previsit( const ParseNode * node ) { |
---|
| 65 | no_strong_cycles.previsit( node ); |
---|
| 66 | set_code_locations.previsit( node ); |
---|
| 67 | } |
---|
| 68 | |
---|
[f69fac7] | 69 | void postvisit( const Node * node ) { |
---|
| 70 | no_strong_cycles.postvisit( node ); |
---|
| 71 | } |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | } // namespace |
---|
| 75 | |
---|
| 76 | void checkInvariants( TranslationUnit & transUnit ) { |
---|
| 77 | ast::Pass<InvariantCore>::run( transUnit ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | } // namespace ast |
---|