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.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 12:19:47 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sun May 17 12:23:36 2015 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include <algorithm> |
---|
17 | #include <iterator> |
---|
18 | |
---|
19 | #include "TypeEnvironment.h" |
---|
20 | #include "SynTree/Type.h" |
---|
21 | #include "SynTree/TypeSubstitution.h" |
---|
22 | #include "Common/utility.h" |
---|
23 | |
---|
24 | namespace ResolvExpr { |
---|
25 | void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent ) { |
---|
26 | for ( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) { |
---|
27 | i->first->print( os, indent ); |
---|
28 | if ( i->second ) { |
---|
29 | os << "(used)"; |
---|
30 | } else { |
---|
31 | os << "(not used)"; |
---|
32 | } // if |
---|
33 | } // for |
---|
34 | } |
---|
35 | |
---|
36 | void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent ) { |
---|
37 | os << std::string( indent, ' ' ); |
---|
38 | for ( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) { |
---|
39 | os << i->first << "(" << i->second << ") "; |
---|
40 | } // for |
---|
41 | } |
---|
42 | |
---|
43 | void EqvClass::initialize( const EqvClass &src, EqvClass &dest ) { |
---|
44 | dest.vars = src.vars; |
---|
45 | dest.type = maybeClone( src.type ); |
---|
46 | dest.allowWidening = src.allowWidening; |
---|
47 | dest.kind = src.kind; |
---|
48 | } |
---|
49 | |
---|
50 | EqvClass::EqvClass() : type( 0 ), allowWidening( true ) { |
---|
51 | } |
---|
52 | |
---|
53 | EqvClass::EqvClass( const EqvClass &other ) { |
---|
54 | initialize( other, *this ); |
---|
55 | } |
---|
56 | |
---|
57 | EqvClass &EqvClass::operator=( const EqvClass &other ) { |
---|
58 | if ( this == &other ) return *this; |
---|
59 | delete type; |
---|
60 | initialize( other, *this ); |
---|
61 | return *this; |
---|
62 | } |
---|
63 | |
---|
64 | EqvClass::~EqvClass() { |
---|
65 | delete type; |
---|
66 | } |
---|
67 | |
---|
68 | void EqvClass::print( std::ostream &os, int indent ) const { |
---|
69 | os << std::string( indent, ' ' ) << "( "; |
---|
70 | std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) ); |
---|
71 | os << ")"; |
---|
72 | if ( type ) { |
---|
73 | os << " -> "; |
---|
74 | type->print( os, indent ); |
---|
75 | } // if |
---|
76 | if ( ! allowWidening ) { |
---|
77 | os << " (no widening)"; |
---|
78 | } // if |
---|
79 | os << std::endl; |
---|
80 | } |
---|
81 | |
---|
82 | bool TypeEnvironment::lookup( const std::string &var, EqvClass &eqvClass ) const { |
---|
83 | for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { |
---|
84 | if ( i->vars.find( var ) != i->vars.end() ) { |
---|
85 | /// std::cout << var << " is in class "; |
---|
86 | /// i->print( std::cout ); |
---|
87 | eqvClass = *i; |
---|
88 | return true; |
---|
89 | } |
---|
90 | /// std::cout << var << " is not in class "; |
---|
91 | /// i->print( std::cout ); |
---|
92 | } // for |
---|
93 | return false; |
---|
94 | } |
---|
95 | |
---|
96 | void TypeEnvironment::add( const EqvClass &eqvClass ) { |
---|
97 | std::list< EqvClass >::iterator i = env.begin(); |
---|
98 | while ( i != env.end() ) { |
---|
99 | std::list< EqvClass >::iterator next = i; |
---|
100 | next++; |
---|
101 | std::set< std::string > intersection; |
---|
102 | std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(), std::inserter( intersection, intersection.begin() ) ); |
---|
103 | if ( ! intersection.empty() ) { |
---|
104 | env.erase( i ); |
---|
105 | } // if |
---|
106 | i = next; |
---|
107 | } // while |
---|
108 | env.insert( env.end(), eqvClass ); |
---|
109 | } |
---|
110 | |
---|
111 | void TypeEnvironment::add( const std::list< TypeDecl* > &tyDecls ) { |
---|
112 | for ( std::list< TypeDecl* >::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) { |
---|
113 | EqvClass newClass; |
---|
114 | newClass.vars.insert( (*i)->get_name() ); |
---|
115 | newClass.kind = (*i)->get_kind(); |
---|
116 | env.push_back( newClass ); |
---|
117 | } // for |
---|
118 | } |
---|
119 | |
---|
120 | void TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const { |
---|
121 | for ( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) { |
---|
122 | for ( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) { |
---|
123 | /// std::cout << "adding " << *theVar; |
---|
124 | if ( theClass->type ) { |
---|
125 | /// std::cout << " bound to "; |
---|
126 | /// theClass->type->print( std::cout ); |
---|
127 | /// std::cout << std::endl; |
---|
128 | sub.add( *theVar, theClass->type ); |
---|
129 | } else if ( theVar != theClass->vars.begin() ) { |
---|
130 | TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->kind == TypeDecl::Ftype ); |
---|
131 | /// std::cout << " bound to variable " << *theClass->vars.begin() << std::endl; |
---|
132 | sub.add( *theVar, newTypeInst ); |
---|
133 | delete newTypeInst; |
---|
134 | } // if |
---|
135 | } // for |
---|
136 | } // for |
---|
137 | /// std::cerr << "input env is:" << std::endl; |
---|
138 | /// print( std::cerr, 8 ); |
---|
139 | /// std::cerr << "sub is:" << std::endl; |
---|
140 | /// sub.print( std::cerr, 8 ); |
---|
141 | sub.normalize(); |
---|
142 | } |
---|
143 | |
---|
144 | void TypeEnvironment::print( std::ostream &os, int indent ) const { |
---|
145 | for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { |
---|
146 | i->print( os, indent ); |
---|
147 | } // for |
---|
148 | } |
---|
149 | |
---|
150 | std::list< EqvClass >::iterator TypeEnvironment::internal_lookup( const std::string &var ) { |
---|
151 | for ( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) { |
---|
152 | if ( i->vars.find( var ) == i->vars.end() ) { |
---|
153 | return i; |
---|
154 | } // if |
---|
155 | } // for |
---|
156 | return env.end(); |
---|
157 | } |
---|
158 | |
---|
159 | void TypeEnvironment::simpleCombine( const TypeEnvironment &second ) { |
---|
160 | env.insert( env.end(), second.env.begin(), second.env.end() ); |
---|
161 | } |
---|
162 | |
---|
163 | void TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ) { |
---|
164 | TypeEnvironment secondCopy( second ); |
---|
165 | for ( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) { |
---|
166 | EqvClass &newClass = *firstClass; |
---|
167 | std::set< std::string > newVars; |
---|
168 | for ( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) { |
---|
169 | std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var ); |
---|
170 | if ( secondClass != secondCopy.env.end() ) { |
---|
171 | newVars.insert( secondClass->vars.begin(), secondClass->vars.end() ); |
---|
172 | if ( secondClass->type ) { |
---|
173 | if ( newClass.type ) { |
---|
174 | Type *newType = combineFunc( newClass.type, secondClass->type ); |
---|
175 | delete newClass.type; |
---|
176 | newClass.type = newType; |
---|
177 | newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening; |
---|
178 | } else { |
---|
179 | newClass.type = secondClass->type->clone(); |
---|
180 | newClass.allowWidening = secondClass->allowWidening; |
---|
181 | } // if |
---|
182 | } // if |
---|
183 | secondCopy.env.erase( secondClass ); |
---|
184 | } // if |
---|
185 | } // for |
---|
186 | newClass.vars.insert( newVars.begin(), newVars.end() ); |
---|
187 | } // for |
---|
188 | for ( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) { |
---|
189 | env.push_back( *secondClass ); |
---|
190 | } // for |
---|
191 | } |
---|
192 | |
---|
193 | void TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const { |
---|
194 | for ( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) { |
---|
195 | for ( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) { |
---|
196 | openVars[ *var ] = eqvClass->kind; |
---|
197 | } // for |
---|
198 | } // for |
---|
199 | } |
---|
200 | |
---|
201 | } // namespace ResolvExpr |
---|
202 | |
---|
203 | // Local Variables: // |
---|
204 | // tab-width: 4 // |
---|
205 | // mode: c++ // |
---|
206 | // compile-command: "make install" // |
---|
207 | // End: // |
---|