[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 | // |
---|
[3f681b1] | 7 | // Util.cpp -- General utilities for working with the AST. |
---|
[f69fac7] | 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Wed Jan 19 9:46:00 2022 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
[3f681b1] | 12 | // Last Modified On : Wed May 4 15:50:00 2022 |
---|
| 13 | // Update Count : 2 |
---|
[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. |
---|
[3f681b1] | 48 | void isCodeLocationSet( const ParseNode * node ) { |
---|
| 49 | assert( node->location.isSet() ); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /// Make sure the reference counts are in a valid combination. |
---|
| 53 | void isStable( const Node * node ) { |
---|
| 54 | assert( node->isStable() ); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /// Check that a FunctionDecl is synchronized with it's FunctionType. |
---|
| 58 | void functionDeclMatchesType( const FunctionDecl * decl ) { |
---|
| 59 | // The type is a cache of sorts, if it is missing that is only a |
---|
| 60 | // problem if isTypeFixed is set. |
---|
| 61 | if ( decl->isTypeFixed ) { |
---|
| 62 | assert( decl->type ); |
---|
| 63 | } else if ( !decl->type ) { |
---|
| 64 | return; |
---|
[33b7d49] | 65 | } |
---|
[3f681b1] | 66 | |
---|
| 67 | const FunctionType * type = decl->type; |
---|
| 68 | |
---|
| 69 | // Check that `type->forall` corresponds with `decl->type_params`. |
---|
| 70 | assert( type->forall.size() == decl->type_params.size() ); |
---|
| 71 | // Check that `type->assertions` corresponds with `decl->assertions`. |
---|
| 72 | assert( type->assertions.size() == decl->assertions.size() ); |
---|
| 73 | // Check that `type->params` corresponds with `decl->params`. |
---|
| 74 | assert( type->params.size() == decl->params.size() ); |
---|
| 75 | // Check that `type->returns` corresponds with `decl->returns`. |
---|
| 76 | assert( type->returns.size() == decl->returns.size() ); |
---|
| 77 | } |
---|
[33b7d49] | 78 | |
---|
[f69fac7] | 79 | struct InvariantCore { |
---|
| 80 | // To save on the number of visits: this is a kind of composed core. |
---|
| 81 | // None of the passes should make changes so ordering doesn't matter. |
---|
| 82 | NoStrongCyclesCore no_strong_cycles; |
---|
| 83 | |
---|
| 84 | void previsit( const Node * node ) { |
---|
| 85 | no_strong_cycles.previsit( node ); |
---|
[3f681b1] | 86 | isStable( node ); |
---|
[f69fac7] | 87 | } |
---|
| 88 | |
---|
[33b7d49] | 89 | void previsit( const ParseNode * node ) { |
---|
[3f681b1] | 90 | previsit( (const Node *)node ); |
---|
| 91 | isCodeLocationSet( node ); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void previsit( const FunctionDecl * node ) { |
---|
| 95 | previsit( (const ParseNode *)node ); |
---|
| 96 | functionDeclMatchesType( node ); |
---|
[33b7d49] | 97 | } |
---|
| 98 | |
---|
[f69fac7] | 99 | void postvisit( const Node * node ) { |
---|
| 100 | no_strong_cycles.postvisit( node ); |
---|
| 101 | } |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | } // namespace |
---|
| 105 | |
---|
| 106 | void checkInvariants( TranslationUnit & transUnit ) { |
---|
| 107 | ast::Pass<InvariantCore>::run( transUnit ); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | } // namespace ast |
---|