[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
|
---|
[7675f58] | 12 | // Last Modified On : Wed May 11 16:16:00 2022
|
---|
| 13 | // Update Count : 3
|
---|
[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 |
|
---|
[7675f58] | 52 | void areLabelLocationsSet( const Stmt * stmt ) {
|
---|
| 53 | for ( const Label& label : stmt->labels ) {
|
---|
| 54 | assert( label.location.isSet() );
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[3f681b1] | 58 | /// Make sure the reference counts are in a valid combination.
|
---|
| 59 | void isStable( const Node * node ) {
|
---|
| 60 | assert( node->isStable() );
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /// Check that a FunctionDecl is synchronized with it's FunctionType.
|
---|
| 64 | void functionDeclMatchesType( const FunctionDecl * decl ) {
|
---|
| 65 | // The type is a cache of sorts, if it is missing that is only a
|
---|
| 66 | // problem if isTypeFixed is set.
|
---|
| 67 | if ( decl->isTypeFixed ) {
|
---|
| 68 | assert( decl->type );
|
---|
| 69 | } else if ( !decl->type ) {
|
---|
| 70 | return;
|
---|
[33b7d49] | 71 | }
|
---|
[3f681b1] | 72 |
|
---|
| 73 | const FunctionType * type = decl->type;
|
---|
| 74 |
|
---|
| 75 | // Check that `type->forall` corresponds with `decl->type_params`.
|
---|
| 76 | assert( type->forall.size() == decl->type_params.size() );
|
---|
| 77 | // Check that `type->assertions` corresponds with `decl->assertions`.
|
---|
| 78 | assert( type->assertions.size() == decl->assertions.size() );
|
---|
| 79 | // Check that `type->params` corresponds with `decl->params`.
|
---|
| 80 | assert( type->params.size() == decl->params.size() );
|
---|
| 81 | // Check that `type->returns` corresponds with `decl->returns`.
|
---|
| 82 | assert( type->returns.size() == decl->returns.size() );
|
---|
| 83 | }
|
---|
[33b7d49] | 84 |
|
---|
[0658672] | 85 | /// Check that the MemberExpr has an aggregate type and matching member.
|
---|
| 86 | void memberMatchesAggregate( const MemberExpr * expr ) {
|
---|
| 87 | const Type * aggrType = expr->aggregate->result->stripReferences();
|
---|
| 88 | const AggregateDecl * decl = nullptr;
|
---|
| 89 | if ( auto inst = dynamic_cast<const StructInstType *>( aggrType ) ) {
|
---|
| 90 | decl = inst->base;
|
---|
| 91 | } else if ( auto inst = dynamic_cast<const UnionInstType *>( aggrType ) ) {
|
---|
| 92 | decl = inst->base;
|
---|
| 93 | }
|
---|
| 94 | assertf( decl, "Aggregate of member not correct type." );
|
---|
| 95 |
|
---|
| 96 | for ( auto aggrMember : decl->members ) {
|
---|
| 97 | if ( expr->member == aggrMember ) {
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | assertf( false, "Member not found." );
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[f69fac7] | 104 | struct InvariantCore {
|
---|
| 105 | // To save on the number of visits: this is a kind of composed core.
|
---|
| 106 | // None of the passes should make changes so ordering doesn't matter.
|
---|
| 107 | NoStrongCyclesCore no_strong_cycles;
|
---|
| 108 |
|
---|
| 109 | void previsit( const Node * node ) {
|
---|
| 110 | no_strong_cycles.previsit( node );
|
---|
[3f681b1] | 111 | isStable( node );
|
---|
[f69fac7] | 112 | }
|
---|
| 113 |
|
---|
[33b7d49] | 114 | void previsit( const ParseNode * node ) {
|
---|
[3f681b1] | 115 | previsit( (const Node *)node );
|
---|
| 116 | isCodeLocationSet( node );
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void previsit( const FunctionDecl * node ) {
|
---|
| 120 | previsit( (const ParseNode *)node );
|
---|
| 121 | functionDeclMatchesType( node );
|
---|
[33b7d49] | 122 | }
|
---|
| 123 |
|
---|
[7675f58] | 124 | void previsit( const Stmt * node ) {
|
---|
| 125 | previsit( (const ParseNode *)node );
|
---|
| 126 | areLabelLocationsSet( node );
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[0658672] | 129 | void previsit( const MemberExpr * node ) {
|
---|
| 130 | previsit( (const ParseNode *)node );
|
---|
| 131 | memberMatchesAggregate( node );
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[f69fac7] | 134 | void postvisit( const Node * node ) {
|
---|
| 135 | no_strong_cycles.postvisit( node );
|
---|
| 136 | }
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | } // namespace
|
---|
| 140 |
|
---|
| 141 | void checkInvariants( TranslationUnit & transUnit ) {
|
---|
| 142 | ast::Pass<InvariantCore>::run( transUnit );
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | } // namespace ast
|
---|