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 |
---|
20 | |
---|
21 | #include "Common/utility.h" // for maybeClone |
---|
22 | #include "SynTree/Type.h" // for Type, FunctionType, Type::Fora... |
---|
23 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
24 | #include "TypeEnvironment.h" |
---|
25 | |
---|
26 | namespace ResolvExpr { |
---|
27 | // adding this comparison operator significantly improves assertion resolution run time for |
---|
28 | // some cases. The current resolution algorithm's speed partially depends on the order of |
---|
29 | // assertions. Assertions which have fewer possible matches should appear before |
---|
30 | // assertions which have more possible matches. This seems to imply that this could |
---|
31 | // be further improved by providing an indexer as an additional argument and ordering based |
---|
32 | // on the number of matches of the same kind (object, function) for the names of the |
---|
33 | // declarations. |
---|
34 | // |
---|
35 | // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this comparator. |
---|
36 | bool AssertCompare::operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const { |
---|
37 | // Objects are always less than functions |
---|
38 | if ( ObjectDecl * objectDecl1 = dynamic_cast< ObjectDecl * >( d1 ) ) { |
---|
39 | if ( ObjectDecl * objectDecl2 = dynamic_cast< ObjectDecl * >( d2 ) ) { |
---|
40 | // objects are ordered by name then type pointer, in that order |
---|
41 | int cmp = objectDecl1->get_name().compare( objectDecl2->get_name() ); |
---|
42 | return cmp < 0 || |
---|
43 | ( cmp == 0 && objectDecl1->get_type() < objectDecl2->get_type() ); |
---|
44 | } else { |
---|
45 | return true; |
---|
46 | } |
---|
47 | } else if ( FunctionDecl * funcDecl1 = dynamic_cast< FunctionDecl * >( d1 ) ) { |
---|
48 | if ( FunctionDecl * funcDecl2 = dynamic_cast< FunctionDecl * >( d2 ) ) { |
---|
49 | // functions are ordered by name, # parameters, # returnVals, type pointer in that order |
---|
50 | FunctionType * ftype1 = funcDecl1->get_functionType(); |
---|
51 | FunctionType * ftype2 = funcDecl2->get_functionType(); |
---|
52 | int numThings1 = ftype1->get_parameters().size() + ftype1->get_returnVals().size(); |
---|
53 | int numThings2 = ftype2->get_parameters().size() + ftype2->get_returnVals().size(); |
---|
54 | if ( numThings1 < numThings2 ) return true; |
---|
55 | if ( numThings1 > numThings2 ) return false; |
---|
56 | |
---|
57 | // if ( ftype1->get_parameters().size() < ftype2->get_parameters().size() ) return true; |
---|
58 | // else if ( ftype1->get_parameters().size() > ftype2->get_parameters().size() ) return false; |
---|
59 | // // same number of parameters |
---|
60 | // if ( ftype1->get_returnVals().size() < ftype2->get_returnVals().size() ) return true; |
---|
61 | // else if ( ftype1->get_returnVals().size() > ftype2->get_returnVals().size() ) return false; |
---|
62 | // same number of return vals |
---|
63 | // int cmp = funcDecl1->get_name().compare( funcDecl2->get_name() ); |
---|
64 | // if ( cmp < 0 ) return true; |
---|
65 | // else if ( cmp > 0 ) return false; |
---|
66 | // // same name |
---|
67 | return ftype1 < ftype2; |
---|
68 | } else { |
---|
69 | return false; |
---|
70 | } |
---|
71 | } else { |
---|
72 | assert( false ); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent ) { |
---|
77 | for ( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) { |
---|
78 | i->first->print( os, indent ); |
---|
79 | if ( i->second.isUsed ) { |
---|
80 | os << "(used)"; |
---|
81 | } else { |
---|
82 | os << "(not used)"; |
---|
83 | } // if |
---|
84 | } // for |
---|
85 | } |
---|
86 | |
---|
87 | void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent ) { |
---|
88 | os << std::string( indent, ' ' ); |
---|
89 | for ( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) { |
---|
90 | os << i->first << "(" << i->second << ") "; |
---|
91 | } // for |
---|
92 | } |
---|
93 | |
---|
94 | void EqvClass::initialize( const EqvClass &src, EqvClass &dest ) { |
---|
95 | dest.vars = src.vars; |
---|
96 | dest.type = maybeClone( src.type ); |
---|
97 | dest.allowWidening = src.allowWidening; |
---|
98 | dest.data = src.data; |
---|
99 | } |
---|
100 | |
---|
101 | EqvClass::EqvClass() : type( 0 ), allowWidening( true ) { |
---|
102 | } |
---|
103 | |
---|
104 | EqvClass::EqvClass( const EqvClass &other ) { |
---|
105 | initialize( other, *this ); |
---|
106 | } |
---|
107 | |
---|
108 | EqvClass &EqvClass::operator=( const EqvClass &other ) { |
---|
109 | if ( this == &other ) return *this; |
---|
110 | delete type; |
---|
111 | initialize( other, *this ); |
---|
112 | return *this; |
---|
113 | } |
---|
114 | |
---|
115 | EqvClass::~EqvClass() { |
---|
116 | delete type; |
---|
117 | } |
---|
118 | |
---|
119 | void EqvClass::print( std::ostream &os, int indent ) const { |
---|
120 | os << std::string( indent, ' ' ) << "( "; |
---|
121 | std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) ); |
---|
122 | os << ")"; |
---|
123 | if ( type ) { |
---|
124 | os << " -> "; |
---|
125 | type->print( os, indent ); |
---|
126 | } // if |
---|
127 | if ( ! allowWidening ) { |
---|
128 | os << " (no widening)"; |
---|
129 | } // if |
---|
130 | os << std::endl; |
---|
131 | } |
---|
132 | |
---|
133 | bool TypeEnvironment::lookup( const std::string &var, EqvClass &eqvClass ) const { |
---|
134 | for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { |
---|
135 | if ( i->vars.find( var ) != i->vars.end() ) { |
---|
136 | /// std::cout << var << " is in class "; |
---|
137 | /// i->print( std::cout ); |
---|
138 | eqvClass = *i; |
---|
139 | return true; |
---|
140 | } |
---|
141 | /// std::cout << var << " is not in class "; |
---|
142 | /// i->print( std::cout ); |
---|
143 | } // for |
---|
144 | return false; |
---|
145 | } |
---|
146 | |
---|
147 | void TypeEnvironment::add( const EqvClass &eqvClass ) { |
---|
148 | std::list< EqvClass >::iterator i = env.begin(); |
---|
149 | while ( i != env.end() ) { |
---|
150 | std::list< EqvClass >::iterator next = i; |
---|
151 | next++; |
---|
152 | std::set< std::string > intersection; |
---|
153 | std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(), std::inserter( intersection, intersection.begin() ) ); |
---|
154 | if ( ! intersection.empty() ) { |
---|
155 | env.erase( i ); |
---|
156 | } // if |
---|
157 | i = next; |
---|
158 | } // while |
---|
159 | env.insert( env.end(), eqvClass ); |
---|
160 | } |
---|
161 | |
---|
162 | void TypeEnvironment::add( const Type::ForallList &tyDecls ) { |
---|
163 | for ( Type::ForallList::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) { |
---|
164 | EqvClass newClass; |
---|
165 | newClass.vars.insert( (*i)->get_name() ); |
---|
166 | newClass.data = TypeDecl::Data{ (*i) }; |
---|
167 | env.push_back( newClass ); |
---|
168 | } // for |
---|
169 | } |
---|
170 | |
---|
171 | void TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const { |
---|
172 | for ( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) { |
---|
173 | for ( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) { |
---|
174 | /// std::cout << "adding " << *theVar; |
---|
175 | if ( theClass->type ) { |
---|
176 | /// std::cout << " bound to "; |
---|
177 | /// theClass->type->print( std::cout ); |
---|
178 | /// std::cout << std::endl; |
---|
179 | sub.add( *theVar, theClass->type ); |
---|
180 | } else if ( theVar != theClass->vars.begin() ) { |
---|
181 | TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->data.kind == TypeDecl::Ftype ); |
---|
182 | /// std::cout << " bound to variable " << *theClass->vars.begin() << std::endl; |
---|
183 | sub.add( *theVar, newTypeInst ); |
---|
184 | delete newTypeInst; |
---|
185 | } // if |
---|
186 | } // for |
---|
187 | } // for |
---|
188 | /// std::cerr << "input env is:" << std::endl; |
---|
189 | /// print( std::cerr, 8 ); |
---|
190 | /// std::cerr << "sub is:" << std::endl; |
---|
191 | /// sub.print( std::cerr, 8 ); |
---|
192 | sub.normalize(); |
---|
193 | } |
---|
194 | |
---|
195 | void TypeEnvironment::print( std::ostream &os, int indent ) const { |
---|
196 | for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) { |
---|
197 | i->print( os, indent ); |
---|
198 | } // for |
---|
199 | } |
---|
200 | |
---|
201 | std::list< EqvClass >::iterator TypeEnvironment::internal_lookup( const std::string &var ) { |
---|
202 | for ( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) { |
---|
203 | if ( i->vars.find( var ) == i->vars.end() ) { |
---|
204 | return i; |
---|
205 | } // if |
---|
206 | } // for |
---|
207 | return env.end(); |
---|
208 | } |
---|
209 | |
---|
210 | void TypeEnvironment::simpleCombine( const TypeEnvironment &second ) { |
---|
211 | env.insert( env.end(), second.env.begin(), second.env.end() ); |
---|
212 | } |
---|
213 | |
---|
214 | void TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ) { |
---|
215 | TypeEnvironment secondCopy( second ); |
---|
216 | for ( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) { |
---|
217 | EqvClass &newClass = *firstClass; |
---|
218 | std::set< std::string > newVars; |
---|
219 | for ( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) { |
---|
220 | std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var ); |
---|
221 | if ( secondClass != secondCopy.env.end() ) { |
---|
222 | newVars.insert( secondClass->vars.begin(), secondClass->vars.end() ); |
---|
223 | if ( secondClass->type ) { |
---|
224 | if ( newClass.type ) { |
---|
225 | Type *newType = combineFunc( newClass.type, secondClass->type ); |
---|
226 | delete newClass.type; |
---|
227 | newClass.type = newType; |
---|
228 | newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening; |
---|
229 | } else { |
---|
230 | newClass.type = secondClass->type->clone(); |
---|
231 | newClass.allowWidening = secondClass->allowWidening; |
---|
232 | } // if |
---|
233 | } // if |
---|
234 | secondCopy.env.erase( secondClass ); |
---|
235 | } // if |
---|
236 | } // for |
---|
237 | newClass.vars.insert( newVars.begin(), newVars.end() ); |
---|
238 | } // for |
---|
239 | for ( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) { |
---|
240 | env.push_back( *secondClass ); |
---|
241 | } // for |
---|
242 | } |
---|
243 | |
---|
244 | void TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const { |
---|
245 | for ( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) { |
---|
246 | for ( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) { |
---|
247 | openVars[ *var ] = eqvClass->data; |
---|
248 | } // for |
---|
249 | } // for |
---|
250 | } |
---|
251 | |
---|
252 | } // namespace ResolvExpr |
---|
253 | |
---|
254 | // Local Variables: // |
---|
255 | // tab-width: 4 // |
---|
256 | // mode: c++ // |
---|
257 | // compile-command: "make install" // |
---|
258 | // End: // |
---|