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 <cassert> // for assert
|
---|
17 | #include <algorithm> // for copy, set_intersection
|
---|
18 | #include <iterator> // for ostream_iterator, insert_iterator
|
---|
19 | #include <utility> // for pair, move
|
---|
20 |
|
---|
21 | #include "Common/PassVisitor.h" // for PassVisitor<GcTracer>
|
---|
22 | #include "Common/utility.h" // for maybeClone
|
---|
23 | #include "SynTree/GcTracer.h" // for PassVisitor<GcTracer>
|
---|
24 | #include "SynTree/Type.h" // for Type, FunctionType, Type::Fora...
|
---|
25 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
---|
26 | #include "TypeEnvironment.h"
|
---|
27 |
|
---|
28 | namespace ResolvExpr {
|
---|
29 | void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent ) {
|
---|
30 | for ( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) {
|
---|
31 | i->first->print( os, indent );
|
---|
32 | if ( i->second.isUsed ) {
|
---|
33 | os << "(used)";
|
---|
34 | } else {
|
---|
35 | os << "(not used)";
|
---|
36 | } // if
|
---|
37 | } // for
|
---|
38 | }
|
---|
39 |
|
---|
40 | void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent ) {
|
---|
41 | os << std::string( indent, ' ' );
|
---|
42 | for ( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) {
|
---|
43 | os << i->first << "(" << i->second << ") ";
|
---|
44 | } // for
|
---|
45 | }
|
---|
46 |
|
---|
47 | void EqvClass::initialize( const EqvClass &src, EqvClass &dest ) {
|
---|
48 | dest.vars = src.vars;
|
---|
49 | dest.type = maybeClone( src.type );
|
---|
50 | dest.allowWidening = src.allowWidening;
|
---|
51 | dest.data = src.data;
|
---|
52 | }
|
---|
53 |
|
---|
54 | EqvClass::EqvClass() : type( 0 ), allowWidening( true ) {
|
---|
55 | }
|
---|
56 |
|
---|
57 | EqvClass::EqvClass( const EqvClass &other ) {
|
---|
58 | initialize( other, *this );
|
---|
59 | }
|
---|
60 |
|
---|
61 | EqvClass &EqvClass::operator=( const EqvClass &other ) {
|
---|
62 | if ( this == &other ) return *this;
|
---|
63 | initialize( other, *this );
|
---|
64 | return *this;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void EqvClass::print( std::ostream &os, Indenter indent ) const {
|
---|
68 | os << "( ";
|
---|
69 | std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) );
|
---|
70 | os << ")";
|
---|
71 | if ( type ) {
|
---|
72 | os << " -> ";
|
---|
73 | type->print( os, indent+1 );
|
---|
74 | } // if
|
---|
75 | if ( ! allowWidening ) {
|
---|
76 | os << " (no widening)";
|
---|
77 | } // if
|
---|
78 | os << std::endl;
|
---|
79 | }
|
---|
80 |
|
---|
81 | const EqvClass* TypeEnvironment::lookup( const std::string &var ) const {
|
---|
82 | for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) {
|
---|
83 | if ( i->vars.find( var ) != i->vars.end() ) {
|
---|
84 | /// std::cout << var << " is in class ";
|
---|
85 | /// i->print( std::cout );
|
---|
86 | return &*i;
|
---|
87 | }
|
---|
88 | /// std::cout << var << " is not in class ";
|
---|
89 | /// i->print( std::cout );
|
---|
90 | } // for
|
---|
91 | return nullptr;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// Removes any class from env that intersects eqvClass
|
---|
95 | void filterOverlappingClasses( std::list<EqvClass> &env, const EqvClass &eqvClass ) {
|
---|
96 | for ( auto i = env.begin(); i != env.end(); ) {
|
---|
97 | auto next = i;
|
---|
98 | ++next;
|
---|
99 | std::set<std::string> intersection;
|
---|
100 | std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(),
|
---|
101 | std::inserter( intersection, intersection.begin() ) );
|
---|
102 | if ( ! intersection.empty() ) { env.erase( i ); }
|
---|
103 | i = next;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | void TypeEnvironment::add( const EqvClass &eqvClass ) {
|
---|
108 | filterOverlappingClasses( env, eqvClass );
|
---|
109 | env.push_back( eqvClass );
|
---|
110 | }
|
---|
111 |
|
---|
112 | void TypeEnvironment::add( EqvClass &&eqvClass ) {
|
---|
113 | filterOverlappingClasses( env, eqvClass );
|
---|
114 | env.push_back( std::move(eqvClass) );
|
---|
115 | }
|
---|
116 |
|
---|
117 | void TypeEnvironment::add( const Type::ForallList &tyDecls ) {
|
---|
118 | for ( Type::ForallList::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) {
|
---|
119 | EqvClass newClass;
|
---|
120 | newClass.vars.insert( (*i)->get_name() );
|
---|
121 | newClass.data = TypeDecl::Data{ (*i) };
|
---|
122 | env.push_back( newClass );
|
---|
123 | } // for
|
---|
124 | }
|
---|
125 |
|
---|
126 | void TypeEnvironment::add( const TypeSubstitution & sub ) {
|
---|
127 | EqvClass newClass;
|
---|
128 | for ( auto p : sub ) {
|
---|
129 | newClass.vars.insert( p.first );
|
---|
130 | newClass.type = p.second->clone();
|
---|
131 | newClass.allowWidening = false;
|
---|
132 | // Minimal assumptions. Not technically correct, but might be good enough, and
|
---|
133 | // is the best we can do at the moment since information is lost in the
|
---|
134 | // transition to TypeSubstitution
|
---|
135 | newClass.data = TypeDecl::Data{ TypeDecl::Dtype, false };
|
---|
136 | add( newClass );
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | void TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const {
|
---|
141 | for ( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) {
|
---|
142 | for ( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) {
|
---|
143 | /// std::cerr << "adding " << *theVar;
|
---|
144 | if ( theClass->type ) {
|
---|
145 | /// std::cerr << " bound to ";
|
---|
146 | /// theClass->type->print( std::cerr );
|
---|
147 | /// std::cerr << std::endl;
|
---|
148 | sub.add( *theVar, theClass->type );
|
---|
149 | } else if ( theVar != theClass->vars.begin() ) {
|
---|
150 | TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->data.kind == TypeDecl::Ftype );
|
---|
151 | /// std::cerr << " bound to variable " << *theClass->vars.begin() << std::endl;
|
---|
152 | sub.add( *theVar, newTypeInst );
|
---|
153 | } // if
|
---|
154 | } // for
|
---|
155 | } // for
|
---|
156 | /// std::cerr << "input env is:" << std::endl;
|
---|
157 | /// print( std::cerr, 8 );
|
---|
158 | /// std::cerr << "sub is:" << std::endl;
|
---|
159 | /// sub.print( std::cerr, 8 );
|
---|
160 | sub.normalize();
|
---|
161 | }
|
---|
162 |
|
---|
163 | void TypeEnvironment::print( std::ostream &os, Indenter indent ) const {
|
---|
164 | for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) {
|
---|
165 | i->print( os, indent );
|
---|
166 | } // for
|
---|
167 | }
|
---|
168 |
|
---|
169 | std::list< EqvClass >::iterator TypeEnvironment::internal_lookup( const std::string &var ) {
|
---|
170 | for ( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) {
|
---|
171 | if ( i->vars.find( var ) == i->vars.end() ) {
|
---|
172 | return i;
|
---|
173 | } // if
|
---|
174 | } // for
|
---|
175 | return env.end();
|
---|
176 | }
|
---|
177 |
|
---|
178 | void TypeEnvironment::simpleCombine( const TypeEnvironment &second ) {
|
---|
179 | env.insert( env.end(), second.env.begin(), second.env.end() );
|
---|
180 | }
|
---|
181 |
|
---|
182 | void TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ) {
|
---|
183 | TypeEnvironment secondCopy( second );
|
---|
184 | for ( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) {
|
---|
185 | EqvClass &newClass = *firstClass;
|
---|
186 | std::set< std::string > newVars;
|
---|
187 | for ( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) {
|
---|
188 | std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var );
|
---|
189 | if ( secondClass != secondCopy.env.end() ) {
|
---|
190 | newVars.insert( secondClass->vars.begin(), secondClass->vars.end() );
|
---|
191 | if ( secondClass->type ) {
|
---|
192 | if ( newClass.type ) {
|
---|
193 | newClass.type = combineFunc( newClass.type, secondClass->type );
|
---|
194 | newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening;
|
---|
195 | } else {
|
---|
196 | newClass.type = secondClass->type->clone();
|
---|
197 | newClass.allowWidening = secondClass->allowWidening;
|
---|
198 | } // if
|
---|
199 | } // if
|
---|
200 | secondCopy.env.erase( secondClass );
|
---|
201 | } // if
|
---|
202 | } // for
|
---|
203 | newClass.vars.insert( newVars.begin(), newVars.end() );
|
---|
204 | } // for
|
---|
205 | for ( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) {
|
---|
206 | env.push_back( *secondClass );
|
---|
207 | } // for
|
---|
208 | }
|
---|
209 |
|
---|
210 | void TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const {
|
---|
211 | for ( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) {
|
---|
212 | for ( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) {
|
---|
213 | openVars[ *var ] = eqvClass->data;
|
---|
214 | } // for
|
---|
215 | } // for
|
---|
216 | }
|
---|
217 |
|
---|
218 | void TypeEnvironment::addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars ) {
|
---|
219 | for ( const EqvClass& c : actualEnv ) {
|
---|
220 | EqvClass c2 = c;
|
---|
221 | c2.allowWidening = false;
|
---|
222 | for ( const std::string& var : c2.vars ) {
|
---|
223 | openVars[ var ] = c2.data;
|
---|
224 | }
|
---|
225 | env.push_back( std::move(c2) );
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ) {
|
---|
230 | env.print( out );
|
---|
231 | return out;
|
---|
232 | }
|
---|
233 |
|
---|
234 | PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env ) {
|
---|
235 | for ( const EqvClass & c : env ) {
|
---|
236 | maybeAccept( c.type, gc );
|
---|
237 | }
|
---|
238 | return gc;
|
---|
239 | }
|
---|
240 | } // namespace ResolvExpr
|
---|
241 |
|
---|
242 | // Local Variables: //
|
---|
243 | // tab-width: 4 //
|
---|
244 | // mode: c++ //
|
---|
245 | // compile-command: "make install" //
|
---|
246 | // End: //
|
---|