source: src/AST/Util.cpp @ 3f681b1

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 3f681b1 was 3f681b1, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Added the new invariant checks. There seems to be a few unset locations in the multi-level exit code.

  • Property mode set to 100644
File size: 2.9 KB
Line 
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  4 15:50:00 2022
13// Update Count     : 2
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
25namespace ast {
26
27namespace {
28
29/// Check that ast::ptr/strong references do not form a cycle.
30struct 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.
48void isCodeLocationSet( const ParseNode * node ) {
49        assert( node->location.isSet() );
50}
51
52/// Make sure the reference counts are in a valid combination.
53void isStable( const Node * node ) {
54        assert( node->isStable() );
55}
56
57/// Check that a FunctionDecl is synchronized with it's FunctionType.
58void 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;
65        }
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}
78
79struct 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 );
86                isStable( node );
87        }
88
89        void previsit( const ParseNode * node ) {
90                previsit( (const Node *)node );
91                isCodeLocationSet( node );
92        }
93
94        void previsit( const FunctionDecl * node ) {
95                previsit( (const ParseNode *)node );
96                functionDeclMatchesType( node );
97        }
98
99        void postvisit( const Node * node ) {
100                no_strong_cycles.postvisit( node );
101        }
102};
103
104} // namespace
105
106void checkInvariants( TranslationUnit & transUnit ) {
107        ast::Pass<InvariantCore>::run( transUnit );
108}
109
110} // namespace ast
Note: See TracBrowser for help on using the repository browser.