| 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 | //
 | 
|---|
| 7 | // TypeEnvironment.h --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Sun May 17 12:24:58 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Sun May 17 12:26:52 2015
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef TYPEENVIRONMENT_H
 | 
|---|
| 17 | #define TYPEENVIRONMENT_H
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <string>
 | 
|---|
| 20 | #include <set>
 | 
|---|
| 21 | #include <list>
 | 
|---|
| 22 | #include <iostream>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "SynTree/SynTree.h"
 | 
|---|
| 25 | #include "SynTree/Type.h"
 | 
|---|
| 26 | #include "SynTree/TypeSubstitution.h"
 | 
|---|
| 27 | #include "SynTree/Declaration.h"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | namespace ResolvExpr {
 | 
|---|
| 30 |         struct AssertCompare {
 | 
|---|
| 31 |                 bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 );
 | 
|---|
| 32 |         };
 | 
|---|
| 33 |         typedef std::map< DeclarationWithType*, bool, AssertCompare > AssertionSet;
 | 
|---|
| 34 |         typedef std::map< std::string, TypeDecl::Kind > OpenVarSet;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
 | 
|---|
| 37 |         void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         struct EqvClass {
 | 
|---|
| 40 |                 std::set< std::string > vars;
 | 
|---|
| 41 |                 Type *type;
 | 
|---|
| 42 |                 bool allowWidening;
 | 
|---|
| 43 |                 TypeDecl::Kind kind;
 | 
|---|
| 44 | 
 | 
|---|
| 45 |                 void initialize( const EqvClass &src, EqvClass &dest );
 | 
|---|
| 46 |                 EqvClass();
 | 
|---|
| 47 |                 EqvClass( const EqvClass &other );
 | 
|---|
| 48 |                 EqvClass &operator=( const EqvClass &other );
 | 
|---|
| 49 |                 ~EqvClass();
 | 
|---|
| 50 |                 void print( std::ostream &os, int indent = 0 ) const;
 | 
|---|
| 51 |         };
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         class TypeEnvironment {
 | 
|---|
| 54 |           public:
 | 
|---|
| 55 |                 bool lookup( const std::string &var, EqvClass &eqvClass ) const;
 | 
|---|
| 56 |                 void add( const EqvClass &eqvClass );
 | 
|---|
| 57 |                 void add( const std::list< TypeDecl* > &tyDecls );
 | 
|---|
| 58 |                 template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
 | 
|---|
| 59 |                 template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
 | 
|---|
| 60 |                 void makeSubstitution( TypeSubstitution &result ) const;
 | 
|---|
| 61 |                 bool isEmpty() const { return env.empty(); }
 | 
|---|
| 62 |                 void print( std::ostream &os, int indent = 0 ) const;
 | 
|---|
| 63 |                 void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
 | 
|---|
| 64 |                 void simpleCombine( const TypeEnvironment &second );
 | 
|---|
| 65 |                 void extractOpenVars( OpenVarSet &openVars ) const;
 | 
|---|
| 66 |                 TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |                 typedef std::list< EqvClass >::iterator iterator;
 | 
|---|
| 69 |                 iterator begin() { return env.begin(); }
 | 
|---|
| 70 |                 iterator end() { return env.end(); }
 | 
|---|
| 71 |                 typedef std::list< EqvClass >::const_iterator const_iterator;
 | 
|---|
| 72 |                 const_iterator begin() const { return env.begin(); }
 | 
|---|
| 73 |                 const_iterator end() const { return env.end(); }
 | 
|---|
| 74 |           private:
 | 
|---|
| 75 |                 std::list< EqvClass > env;
 | 
|---|
| 76 |                 std::list< EqvClass >::iterator internal_lookup( const std::string &var );
 | 
|---|
| 77 |         };
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         template< typename SynTreeClass >
 | 
|---|
| 80 |         int TypeEnvironment::apply( SynTreeClass *&type ) const {
 | 
|---|
| 81 |                 TypeSubstitution sub;
 | 
|---|
| 82 |                 makeSubstitution( sub );
 | 
|---|
| 83 |                 return sub.apply( type );
 | 
|---|
| 84 |         }
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         template< typename SynTreeClass >
 | 
|---|
| 87 |         int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
 | 
|---|
| 88 |                 TypeSubstitution sub;
 | 
|---|
| 89 |                 makeSubstitution( sub );
 | 
|---|
| 90 |                 return sub.applyFree( type );
 | 
|---|
| 91 |         }
 | 
|---|
| 92 | } // namespace ResolvExpr
 | 
|---|
| 93 | 
 | 
|---|
| 94 | #endif // TYPEENVIRONMENT_H */
 | 
|---|
| 95 | 
 | 
|---|
| 96 | // Local Variables: //
 | 
|---|
| 97 | // tab-width: 4 //
 | 
|---|
| 98 | // mode: c++ //
 | 
|---|
| 99 | // compile-command: "make install" //
 | 
|---|
| 100 | // End: //
 | 
|---|