[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 | // |
---|
[03da511] | 7 | // TypeEnvironment.h -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:24:58 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[6b0b624] | 12 | // Last Modified On : Sat Jul 22 09:35:45 2017 |
---|
| 13 | // Update Count : 3 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
| 18 | #include <string> |
---|
| 19 | #include <set> |
---|
| 20 | #include <list> |
---|
| 21 | #include <iostream> |
---|
| 22 | |
---|
| 23 | #include "SynTree/SynTree.h" |
---|
| 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/TypeSubstitution.h" |
---|
| 26 | #include "SynTree/Declaration.h" |
---|
| 27 | |
---|
| 28 | namespace ResolvExpr { |
---|
[03da511] | 29 | struct AssertCompare { |
---|
[fc638d2] | 30 | bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const; |
---|
[03da511] | 31 | }; |
---|
[6c3a988f] | 32 | struct AssertionSetValue { |
---|
| 33 | bool isUsed; |
---|
| 34 | // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID of an assertion on the current type, |
---|
| 35 | // with each successive ID being the ID of an assertion pulled in by the previous ID. The last ID in the chain is |
---|
| 36 | // the ID of the assertion that pulled in the current assertion. |
---|
| 37 | std::list< UniqueId > idChain; |
---|
| 38 | }; |
---|
| 39 | typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet; |
---|
[2c57025] | 40 | typedef std::map< std::string, TypeDecl::Data > OpenVarSet; |
---|
[51b7345] | 41 | |
---|
[a32b204] | 42 | void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 ); |
---|
| 43 | void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 ); |
---|
[51b7345] | 44 | |
---|
[a32b204] | 45 | struct EqvClass { |
---|
| 46 | std::set< std::string > vars; |
---|
| 47 | Type *type; |
---|
| 48 | bool allowWidening; |
---|
[2c57025] | 49 | TypeDecl::Data data; |
---|
[03da511] | 50 | |
---|
[a32b204] | 51 | void initialize( const EqvClass &src, EqvClass &dest ); |
---|
| 52 | EqvClass(); |
---|
| 53 | EqvClass( const EqvClass &other ); |
---|
| 54 | EqvClass &operator=( const EqvClass &other ); |
---|
| 55 | ~EqvClass(); |
---|
| 56 | void print( std::ostream &os, int indent = 0 ) const; |
---|
| 57 | }; |
---|
[51b7345] | 58 | |
---|
[a32b204] | 59 | class TypeEnvironment { |
---|
| 60 | public: |
---|
| 61 | bool lookup( const std::string &var, EqvClass &eqvClass ) const; |
---|
| 62 | void add( const EqvClass &eqvClass ); |
---|
[8c49c0e] | 63 | void add( const Type::ForallList &tyDecls ); |
---|
[a32b204] | 64 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const; |
---|
| 65 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const; |
---|
| 66 | void makeSubstitution( TypeSubstitution &result ) const; |
---|
| 67 | bool isEmpty() const { return env.empty(); } |
---|
| 68 | void print( std::ostream &os, int indent = 0 ) const; |
---|
| 69 | void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ); |
---|
| 70 | void simpleCombine( const TypeEnvironment &second ); |
---|
| 71 | void extractOpenVars( OpenVarSet &openVars ) const; |
---|
| 72 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); } |
---|
[03da511] | 73 | |
---|
[a32b204] | 74 | typedef std::list< EqvClass >::iterator iterator; |
---|
| 75 | iterator begin() { return env.begin(); } |
---|
| 76 | iterator end() { return env.end(); } |
---|
| 77 | typedef std::list< EqvClass >::const_iterator const_iterator; |
---|
| 78 | const_iterator begin() const { return env.begin(); } |
---|
| 79 | const_iterator end() const { return env.end(); } |
---|
| 80 | private: |
---|
| 81 | std::list< EqvClass > env; |
---|
| 82 | std::list< EqvClass >::iterator internal_lookup( const std::string &var ); |
---|
| 83 | }; |
---|
[51b7345] | 84 | |
---|
[a32b204] | 85 | template< typename SynTreeClass > |
---|
| 86 | int TypeEnvironment::apply( SynTreeClass *&type ) const { |
---|
| 87 | TypeSubstitution sub; |
---|
| 88 | makeSubstitution( sub ); |
---|
| 89 | return sub.apply( type ); |
---|
| 90 | } |
---|
[51b7345] | 91 | |
---|
[a32b204] | 92 | template< typename SynTreeClass > |
---|
| 93 | int TypeEnvironment::applyFree( SynTreeClass *&type ) const { |
---|
| 94 | TypeSubstitution sub; |
---|
| 95 | makeSubstitution( sub ); |
---|
| 96 | return sub.applyFree( type ); |
---|
| 97 | } |
---|
[51b7345] | 98 | } // namespace ResolvExpr |
---|
| 99 | |
---|
[a32b204] | 100 | // Local Variables: // |
---|
| 101 | // tab-width: 4 // |
---|
| 102 | // mode: c++ // |
---|
| 103 | // compile-command: "make install" // |
---|
| 104 | // End: // |
---|