1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * $Id: TypeEnvironment.h,v 1.8 2005/08/29 20:14:16 rcbilson Exp $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef RESOLVEXPR_TYPEENVIRONMENT_H |
---|
9 | #define RESOLVEXPR_TYPEENVIRONMENT_H |
---|
10 | |
---|
11 | #include <string> |
---|
12 | #include <set> |
---|
13 | #include <list> |
---|
14 | #include <iostream> |
---|
15 | |
---|
16 | #include "SynTree/SynTree.h" |
---|
17 | #include "SynTree/Type.h" |
---|
18 | #include "SynTree/TypeSubstitution.h" |
---|
19 | #include "SynTree/Declaration.h" |
---|
20 | |
---|
21 | namespace ResolvExpr { |
---|
22 | |
---|
23 | typedef std::map< DeclarationWithType*, bool > AssertionSet; |
---|
24 | typedef std::map< std::string, TypeDecl::Kind > OpenVarSet; |
---|
25 | |
---|
26 | void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 ); |
---|
27 | void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 ); |
---|
28 | |
---|
29 | struct EqvClass |
---|
30 | { |
---|
31 | std::set< std::string > vars; |
---|
32 | Type *type; |
---|
33 | bool allowWidening; |
---|
34 | TypeDecl::Kind kind; |
---|
35 | |
---|
36 | void initialize( const EqvClass &src, EqvClass &dest ); |
---|
37 | EqvClass(); |
---|
38 | EqvClass( const EqvClass &other ); |
---|
39 | EqvClass &operator=( const EqvClass &other ); |
---|
40 | ~EqvClass(); |
---|
41 | void print( std::ostream &os, int indent = 0 ) const; |
---|
42 | }; |
---|
43 | |
---|
44 | class TypeEnvironment |
---|
45 | { |
---|
46 | public: |
---|
47 | bool lookup( const std::string &var, EqvClass &eqvClass ) const; |
---|
48 | void add( const EqvClass &eqvClass ); |
---|
49 | void add( const std::list< TypeDecl* > &tyDecls ); |
---|
50 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const; |
---|
51 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const; |
---|
52 | void makeSubstitution( TypeSubstitution &result ) const; |
---|
53 | bool isEmpty() const { return env.empty(); } |
---|
54 | void print( std::ostream &os, int indent = 0 ) const; |
---|
55 | void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ); |
---|
56 | void simpleCombine( const TypeEnvironment &second ); |
---|
57 | void extractOpenVars( OpenVarSet &openVars ) const; |
---|
58 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); } |
---|
59 | |
---|
60 | typedef std::list< EqvClass >::iterator iterator; |
---|
61 | iterator begin() { return env.begin(); } |
---|
62 | iterator end() { return env.end(); } |
---|
63 | typedef std::list< EqvClass >::const_iterator const_iterator; |
---|
64 | const_iterator begin() const { return env.begin(); } |
---|
65 | const_iterator end() const { return env.end(); } |
---|
66 | private: |
---|
67 | std::list< EqvClass > env; |
---|
68 | |
---|
69 | std::list< EqvClass >::iterator internal_lookup( const std::string &var ); |
---|
70 | }; |
---|
71 | |
---|
72 | template< typename SynTreeClass > |
---|
73 | int |
---|
74 | TypeEnvironment::apply( SynTreeClass *&type ) const |
---|
75 | { |
---|
76 | TypeSubstitution sub; |
---|
77 | makeSubstitution( sub ); |
---|
78 | return sub.apply( type ); |
---|
79 | } |
---|
80 | |
---|
81 | template< typename SynTreeClass > |
---|
82 | int |
---|
83 | TypeEnvironment::applyFree( SynTreeClass *&type ) const |
---|
84 | { |
---|
85 | TypeSubstitution sub; |
---|
86 | makeSubstitution( sub ); |
---|
87 | return sub.applyFree( type ); |
---|
88 | } |
---|
89 | |
---|
90 | } // namespace ResolvExpr |
---|
91 | |
---|
92 | #endif /* #ifndef RESOLVEXPR_TYPEENVIRONMENT_H */ |
---|