source: src/ResolvExpr/TypeEnvironment.cc@ 7f623d6f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 7f623d6f was 6c3a988f, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix inferred parameter data structures to correctly associate parameters with the entity that requested them, modify tuple specialization and unification to work with self-recursive assertions

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