| 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 | typedef std::map< DeclarationWithType*, bool > AssertionSet;
|
|---|
| 31 | typedef std::map< std::string, TypeDecl::Kind > OpenVarSet;
|
|---|
| 32 |
|
|---|
| 33 | void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
|
|---|
| 34 | void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
|
|---|
| 35 |
|
|---|
| 36 | struct EqvClass {
|
|---|
| 37 | std::set< std::string > vars;
|
|---|
| 38 | Type *type;
|
|---|
| 39 | bool allowWidening;
|
|---|
| 40 | TypeDecl::Kind kind;
|
|---|
| 41 |
|
|---|
| 42 | void initialize( const EqvClass &src, EqvClass &dest );
|
|---|
| 43 | EqvClass();
|
|---|
| 44 | EqvClass( const EqvClass &other );
|
|---|
| 45 | EqvClass &operator=( const EqvClass &other );
|
|---|
| 46 | ~EqvClass();
|
|---|
| 47 | void print( std::ostream &os, int indent = 0 ) const;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | class TypeEnvironment {
|
|---|
| 51 | public:
|
|---|
| 52 | bool lookup( const std::string &var, EqvClass &eqvClass ) const;
|
|---|
| 53 | void add( const EqvClass &eqvClass );
|
|---|
| 54 | void add( const std::list< TypeDecl* > &tyDecls );
|
|---|
| 55 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
|
|---|
| 56 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
|
|---|
| 57 | void makeSubstitution( TypeSubstitution &result ) const;
|
|---|
| 58 | bool isEmpty() const { return env.empty(); }
|
|---|
| 59 | void print( std::ostream &os, int indent = 0 ) const;
|
|---|
| 60 | void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
|
|---|
| 61 | void simpleCombine( const TypeEnvironment &second );
|
|---|
| 62 | void extractOpenVars( OpenVarSet &openVars ) const;
|
|---|
| 63 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
|
|---|
| 64 |
|
|---|
| 65 | typedef std::list< EqvClass >::iterator iterator;
|
|---|
| 66 | iterator begin() { return env.begin(); }
|
|---|
| 67 | iterator end() { return env.end(); }
|
|---|
| 68 | typedef std::list< EqvClass >::const_iterator const_iterator;
|
|---|
| 69 | const_iterator begin() const { return env.begin(); }
|
|---|
| 70 | const_iterator end() const { return env.end(); }
|
|---|
| 71 | private:
|
|---|
| 72 | std::list< EqvClass > env;
|
|---|
| 73 | std::list< EqvClass >::iterator internal_lookup( const std::string &var );
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | template< typename SynTreeClass >
|
|---|
| 77 | int TypeEnvironment::apply( SynTreeClass *&type ) const {
|
|---|
| 78 | TypeSubstitution sub;
|
|---|
| 79 | makeSubstitution( sub );
|
|---|
| 80 | return sub.apply( type );
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | template< typename SynTreeClass >
|
|---|
| 84 | int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
|
|---|
| 85 | TypeSubstitution sub;
|
|---|
| 86 | makeSubstitution( sub );
|
|---|
| 87 | return sub.applyFree( type );
|
|---|
| 88 | }
|
|---|
| 89 | } // namespace ResolvExpr
|
|---|
| 90 |
|
|---|
| 91 | #endif // TYPEENVIRONMENT_H */
|
|---|
| 92 |
|
|---|
| 93 | // Local Variables: //
|
|---|
| 94 | // tab-width: 4 //
|
|---|
| 95 | // mode: c++ //
|
|---|
| 96 | // compile-command: "make install" //
|
|---|
| 97 | // End: //
|
|---|