source: src/ResolvExpr/TypeEnvironment.cc@ 33f5b57

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 with_gc
Last change on this file since 33f5b57 was 09c72d5, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add function to add bindings from TypeSubstitution to TypeEnvironment

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