[a32b204] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 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 | // |
---|
[8c49c0e] | 7 | // FindOpenVars.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 09:42:48 2015 |
---|
[85dac33] | 11 | // Last Modified By : Andrew |
---|
| 12 | // Last Modified On : Fri Jul 12 14:18:00 2019 |
---|
| 13 | // Update Count : 4 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "FindOpenVars.h" |
---|
[ea6332d] | 17 | |
---|
| 18 | #include <list> // for _List_const_iterator, list<>::const... |
---|
| 19 | #include <map> // for map<>::mapped_type |
---|
| 20 | |
---|
[9519aba] | 21 | #include "AST/Pass.hpp" |
---|
| 22 | #include "AST/Type.hpp" |
---|
[46da46b] | 23 | #include "AST/TypeEnvironment.hpp" |
---|
[ce7ed2c] | 24 | #include "Common/PassVisitor.h" |
---|
[ea6332d] | 25 | #include "SynTree/Declaration.h" // for TypeDecl, DeclarationWithType (ptr ... |
---|
| 26 | #include "SynTree/Type.h" // for Type, Type::ForallList, ArrayType |
---|
[51b7345] | 27 | |
---|
[46da46b] | 28 | #include <iostream> |
---|
| 29 | |
---|
[51b7345] | 30 | namespace ResolvExpr { |
---|
[9519aba] | 31 | struct FindOpenVars_old : public WithGuards { |
---|
| 32 | FindOpenVars_old( OpenVarSet &openVars, OpenVarSet &closedVars, AssertionSet &needAssertions, AssertionSet &haveAssertions, bool firstIsOpen ); |
---|
[a32b204] | 33 | |
---|
[85dac33] | 34 | void previsit( const PointerType * pointerType ); |
---|
| 35 | void previsit( const ArrayType * arrayType ); |
---|
| 36 | void previsit( const FunctionType * functionType ); |
---|
| 37 | void previsit( const TupleType * tupleType ); |
---|
[a32b204] | 38 | |
---|
[85dac33] | 39 | void common_action( const Type *type ); |
---|
[51b7345] | 40 | |
---|
[a32b204] | 41 | OpenVarSet &openVars, &closedVars; |
---|
| 42 | AssertionSet &needAssertions, &haveAssertions; |
---|
| 43 | bool nextIsOpen; |
---|
| 44 | }; |
---|
[51b7345] | 45 | |
---|
[85dac33] | 46 | void findOpenVars( const Type *type, OpenVarSet &openVars, OpenVarSet &closedVars, AssertionSet &needAssertions, AssertionSet &haveAssertions, bool firstIsOpen ) { |
---|
[9519aba] | 47 | PassVisitor<FindOpenVars_old> finder( openVars, closedVars, needAssertions, haveAssertions, firstIsOpen ); |
---|
[a32b204] | 48 | type->accept( finder ); |
---|
| 49 | } |
---|
[51b7345] | 50 | |
---|
[9519aba] | 51 | FindOpenVars_old::FindOpenVars_old( OpenVarSet &openVars, OpenVarSet &closedVars, AssertionSet &needAssertions, AssertionSet &haveAssertions, bool firstIsOpen ) |
---|
[a32b204] | 52 | : openVars( openVars ), closedVars( closedVars ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), nextIsOpen( firstIsOpen ) { |
---|
| 53 | } |
---|
[51b7345] | 54 | |
---|
[85dac33] | 55 | void FindOpenVars_old::common_action( const Type * type ) { |
---|
[a32b204] | 56 | if ( nextIsOpen ) { |
---|
[85dac33] | 57 | for ( Type::ForallList::const_iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { |
---|
[2c57025] | 58 | openVars[ (*i)->get_name() ] = TypeDecl::Data{ (*i) }; |
---|
[a32b204] | 59 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { |
---|
[6c3a988f] | 60 | needAssertions[ *assert ].isUsed = false; |
---|
[a32b204] | 61 | } |
---|
[51b7345] | 62 | /// cloneAll( (*i)->get_assertions(), needAssertions ); |
---|
| 63 | /// needAssertions.insert( needAssertions.end(), (*i)->get_assertions().begin(), (*i)->get_assertions().end() ); |
---|
[a32b204] | 64 | } |
---|
| 65 | } else { |
---|
[85dac33] | 66 | for ( Type::ForallList::const_iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { |
---|
[2c57025] | 67 | closedVars[ (*i)->get_name() ] = TypeDecl::Data{ (*i) }; |
---|
[a32b204] | 68 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) { |
---|
[6c3a988f] | 69 | haveAssertions[ *assert ].isUsed = false; |
---|
[a32b204] | 70 | } |
---|
[51b7345] | 71 | /// cloneAll( (*i)->get_assertions(), haveAssertions ); |
---|
| 72 | /// haveAssertions.insert( haveAssertions.end(), (*i)->get_assertions().begin(), (*i)->get_assertions().end() ); |
---|
[a32b204] | 73 | } // for |
---|
| 74 | } // if |
---|
[ce7ed2c] | 75 | /// std::cerr << "type is "; |
---|
| 76 | /// type->print( std::cerr ); |
---|
| 77 | /// std::cerr << std::endl << "need is" << std::endl; |
---|
| 78 | /// printAssertionSet( needAssertions, std::cerr ); |
---|
| 79 | /// std::cerr << std::endl << "have is" << std::endl; |
---|
| 80 | /// printAssertionSet( haveAssertions, std::cerr ); |
---|
[a32b204] | 81 | } |
---|
[51b7345] | 82 | |
---|
[85dac33] | 83 | void FindOpenVars_old::previsit(const PointerType * pointerType) { |
---|
[a32b204] | 84 | common_action( pointerType ); |
---|
| 85 | } |
---|
[51b7345] | 86 | |
---|
[85dac33] | 87 | void FindOpenVars_old::previsit(const ArrayType * arrayType) { |
---|
[a32b204] | 88 | common_action( arrayType ); |
---|
| 89 | } |
---|
[51b7345] | 90 | |
---|
[85dac33] | 91 | void FindOpenVars_old::previsit(const FunctionType * functionType) { |
---|
[a32b204] | 92 | common_action( functionType ); |
---|
| 93 | nextIsOpen = ! nextIsOpen; |
---|
[ce7ed2c] | 94 | GuardAction( [this](){ nextIsOpen = ! nextIsOpen; } ); |
---|
[a32b204] | 95 | } |
---|
[51b7345] | 96 | |
---|
[85dac33] | 97 | void FindOpenVars_old::previsit(const TupleType * tupleType) { |
---|
[a32b204] | 98 | common_action( tupleType ); |
---|
| 99 | } |
---|
[f474e91] | 100 | |
---|
[9519aba] | 101 | namespace { |
---|
| 102 | struct FindOpenVars_new final : public ast::WithGuards { |
---|
| 103 | ast::OpenVarSet & open; |
---|
| 104 | ast::OpenVarSet & closed; |
---|
| 105 | ast::AssertionSet & need; |
---|
| 106 | ast::AssertionSet & have; |
---|
[46da46b] | 107 | ast::TypeEnvironment & env; |
---|
[9519aba] | 108 | bool nextIsOpen; |
---|
| 109 | |
---|
[85dac33] | 110 | FindOpenVars_new( |
---|
| 111 | ast::OpenVarSet & o, ast::OpenVarSet & c, ast::AssertionSet & n, |
---|
[46da46b] | 112 | ast::AssertionSet & h, ast::TypeEnvironment & env, FirstMode firstIsOpen ) |
---|
| 113 | : open( o ), closed( c ), need( n ), have( h ), env (env), nextIsOpen( firstIsOpen ) {} |
---|
[9519aba] | 114 | |
---|
| 115 | void previsit( const ast::FunctionType * type ) { |
---|
| 116 | // mark open/closed variables |
---|
| 117 | if ( nextIsOpen ) { |
---|
[46da46b] | 118 | // trying to remove this from resolver. |
---|
| 119 | // occasionally used in other parts so not deleting right now. |
---|
| 120 | |
---|
| 121 | // insert open variables unbound to environment. |
---|
| 122 | env.add(type->forall); |
---|
| 123 | |
---|
[3e5dd913] | 124 | for ( auto & decl : type->forall ) { |
---|
[93c10de] | 125 | open[ *decl ] = ast::TypeData{ decl->base }; |
---|
[3e5dd913] | 126 | } |
---|
| 127 | for ( auto & assert : type->assertions ) { |
---|
| 128 | need[ assert ].isUsed = false; |
---|
[9519aba] | 129 | } |
---|
| 130 | } else { |
---|
[3e5dd913] | 131 | for ( auto & decl : type->forall ) { |
---|
[93c10de] | 132 | closed[ *decl ] = ast::TypeData{ decl->base }; |
---|
[3e5dd913] | 133 | } |
---|
| 134 | for ( auto & assert : type->assertions ) { |
---|
| 135 | have[ assert ].isUsed = false; |
---|
[9519aba] | 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // flip open variables for contained function types |
---|
| 140 | nextIsOpen = ! nextIsOpen; |
---|
| 141 | GuardAction( [this](){ nextIsOpen = ! nextIsOpen; } ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | }; |
---|
| 145 | } |
---|
| 146 | |
---|
[85dac33] | 147 | void findOpenVars( |
---|
| 148 | const ast::Type * type, ast::OpenVarSet & open, ast::OpenVarSet & closed, |
---|
[46da46b] | 149 | ast::AssertionSet & need, ast::AssertionSet & have, ast::TypeEnvironment & env, FirstMode firstIsOpen ) { |
---|
| 150 | ast::Pass< FindOpenVars_new > finder{ open, closed, need, have, env, firstIsOpen }; |
---|
[9519aba] | 151 | type->accept( finder ); |
---|
[46da46b] | 152 | |
---|
| 153 | if (!closed.empty()) { |
---|
| 154 | std::cerr << "closed: "; |
---|
| 155 | for (auto& i : closed) { |
---|
| 156 | std::cerr << i.first.base->location << ":" << i.first.base->name << ' '; |
---|
| 157 | } |
---|
| 158 | std::cerr << std::endl; |
---|
| 159 | } |
---|
[f474e91] | 160 | } |
---|
[51b7345] | 161 | } // namespace ResolvExpr |
---|
[a32b204] | 162 | |
---|
| 163 | // Local Variables: // |
---|
| 164 | // tab-width: 4 // |
---|
| 165 | // mode: c++ // |
---|
| 166 | // compile-command: "make install" // |
---|
| 167 | // End: // |
---|