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.cpp -- 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
|
---|
12 | // Last Modified On : Wed May 11 16:16:00 2022
|
---|
13 | // Update Count : 3
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "Util.hpp"
|
---|
17 |
|
---|
18 | #include "Node.hpp"
|
---|
19 | #include "ParseNode.hpp"
|
---|
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 |
|
---|
47 | /// Check that every note that can has a set CodeLocation.
|
---|
48 | void isCodeLocationSet( const ParseNode * node ) {
|
---|
49 | assert( node->location.isSet() );
|
---|
50 | }
|
---|
51 |
|
---|
52 | void areLabelLocationsSet( const Stmt * stmt ) {
|
---|
53 | for ( const Label& label : stmt->labels ) {
|
---|
54 | assert( label.location.isSet() );
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
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;
|
---|
71 | }
|
---|
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 | }
|
---|
84 |
|
---|
85 | struct InvariantCore {
|
---|
86 | // To save on the number of visits: this is a kind of composed core.
|
---|
87 | // None of the passes should make changes so ordering doesn't matter.
|
---|
88 | NoStrongCyclesCore no_strong_cycles;
|
---|
89 |
|
---|
90 | void previsit( const Node * node ) {
|
---|
91 | no_strong_cycles.previsit( node );
|
---|
92 | isStable( node );
|
---|
93 | }
|
---|
94 |
|
---|
95 | void previsit( const ParseNode * node ) {
|
---|
96 | previsit( (const Node *)node );
|
---|
97 | isCodeLocationSet( node );
|
---|
98 | }
|
---|
99 |
|
---|
100 | void previsit( const FunctionDecl * node ) {
|
---|
101 | previsit( (const ParseNode *)node );
|
---|
102 | functionDeclMatchesType( node );
|
---|
103 | }
|
---|
104 |
|
---|
105 | void previsit( const Stmt * node ) {
|
---|
106 | previsit( (const ParseNode *)node );
|
---|
107 | areLabelLocationsSet( node );
|
---|
108 | }
|
---|
109 |
|
---|
110 | void postvisit( const Node * node ) {
|
---|
111 | no_strong_cycles.postvisit( node );
|
---|
112 | }
|
---|
113 | };
|
---|
114 |
|
---|
115 | } // namespace
|
---|
116 |
|
---|
117 | void checkInvariants( TranslationUnit & transUnit ) {
|
---|
118 | ast::Pass<InvariantCore>::run( transUnit );
|
---|
119 | }
|
---|
120 |
|
---|
121 | } // namespace ast
|
---|