source: src/ResolvExpr/TypeEnvironment.cc @ 75308bcc

new-envwith_gc
Last change on this file since 75308bcc was f229fc2, checked in by Aaron Moss <a3moss@…>, 6 years ago

Modify resolver to use young-generation collection per-top-level expression

  • Property mode set to 100644
File size: 8.2 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/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
28namespace 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        bool TypeEnvironment::lookup( const std::string &var, EqvClass &eqvClass ) 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                                eqvClass = *i;
87                                return true;
88                        }
89///     std::cout << var << " is not in class ";
90///     i->print( std::cout );
91                } // for
92                return false;
93        }
94
95        void TypeEnvironment::add( const EqvClass &eqvClass ) {
96                std::list< EqvClass >::iterator i = env.begin();
97                while ( i != env.end() ) {
98                        std::list< EqvClass >::iterator next = i;
99                        next++;
100                        std::set< std::string > intersection;
101                        std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(), std::inserter( intersection, intersection.begin() ) );
102                        if ( ! intersection.empty() ) {
103                                env.erase( i );
104                        } // if
105                        i = next;
106                } // while
107                env.insert( env.end(), eqvClass );
108        }
109
110        void TypeEnvironment::add( const Type::ForallList &tyDecls ) {
111                for ( Type::ForallList::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) {
112                        EqvClass newClass;
113                        newClass.vars.insert( (*i)->get_name() );
114                        newClass.data = TypeDecl::Data{ (*i) };
115                        env.push_back( newClass );
116                } // for
117        }
118
119        void TypeEnvironment::add( const TypeSubstitution & sub ) {
120                EqvClass newClass;
121                for ( auto p : sub ) {
122                        newClass.vars.insert( p.first );
123                        newClass.type = p.second->clone();
124                        newClass.allowWidening = false;
125                        // Minimal assumptions. Not technically correct, but might be good enough, and
126                        // is the best we can do at the moment since information is lost in the
127                        // transition to TypeSubstitution
128                        newClass.data = TypeDecl::Data{ TypeDecl::Dtype, false };
129                        add( newClass );
130                }
131        }
132
133        void TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const {
134                for ( std::list< EqvClass >::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) {
135                        for ( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) {
136///       std::cerr << "adding " << *theVar;
137                                if ( theClass->type ) {
138///         std::cerr << " bound to ";
139///         theClass->type->print( std::cerr );
140///         std::cerr << std::endl;
141                                        sub.add( *theVar, theClass->type );
142                                } else if ( theVar != theClass->vars.begin() ) {
143                                        TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->data.kind == TypeDecl::Ftype );
144///         std::cerr << " bound to variable " << *theClass->vars.begin() << std::endl;
145                                        sub.add( *theVar, newTypeInst );
146                                } // if
147                        } // for
148                } // for
149///   std::cerr << "input env is:" << std::endl;
150///   print( std::cerr, 8 );
151///   std::cerr << "sub is:" << std::endl;
152///   sub.print( std::cerr, 8 );
153                sub.normalize();
154        }
155
156        void TypeEnvironment::print( std::ostream &os, Indenter indent ) const {
157                for ( std::list< EqvClass >::const_iterator i = env.begin(); i != env.end(); ++i ) {
158                        i->print( os, indent );
159                } // for
160        }
161
162        std::list< EqvClass >::iterator TypeEnvironment::internal_lookup( const std::string &var ) {
163                for ( std::list< EqvClass >::iterator i = env.begin(); i != env.end(); ++i ) {
164                        if ( i->vars.find( var ) == i->vars.end() ) {
165                                return i;
166                        } // if
167                } // for
168                return env.end();
169        }
170
171        void TypeEnvironment::simpleCombine( const TypeEnvironment &second ) {
172                env.insert( env.end(), second.env.begin(), second.env.end() );
173        }
174
175        void TypeEnvironment::combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ) {
176                TypeEnvironment secondCopy( second );
177                for ( std::list< EqvClass >::iterator firstClass = env.begin(); firstClass != env.end(); ++firstClass ) {
178                        EqvClass &newClass = *firstClass;
179                        std::set< std::string > newVars;
180                        for ( std::set< std::string >::const_iterator var = firstClass->vars.begin(); var != firstClass->vars.end(); ++var ) {
181                                std::list< EqvClass >::iterator secondClass = secondCopy.internal_lookup( *var );
182                                if ( secondClass != secondCopy.env.end() ) {
183                                        newVars.insert( secondClass->vars.begin(), secondClass->vars.end() );
184                                        if ( secondClass->type ) {
185                                                if ( newClass.type ) {
186                                                        newClass.type = combineFunc( newClass.type, secondClass->type );
187                                                        newClass.allowWidening = newClass.allowWidening && secondClass->allowWidening;
188                                                } else {
189                                                        newClass.type = secondClass->type->clone();
190                                                        newClass.allowWidening = secondClass->allowWidening;
191                                                } // if
192                                        } // if
193                                        secondCopy.env.erase( secondClass );
194                                } // if
195                        } // for
196                        newClass.vars.insert( newVars.begin(), newVars.end() );
197                } // for
198                for ( std::list< EqvClass >::iterator secondClass = secondCopy.env.begin(); secondClass != secondCopy.env.end(); ++secondClass ) {
199                        env.push_back( *secondClass );
200                } // for
201        }
202
203        void TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const {
204                for ( std::list< EqvClass >::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) {
205                        for ( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) {
206                                openVars[ *var ] = eqvClass->data;
207                        } // for
208                } // for
209        }
210
211        void TypeEnvironment::addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars ) {
212                for ( const EqvClass& c : actualEnv ) {
213                        EqvClass c2 = c;
214                        c2.allowWidening = false;
215                        for ( const std::string& var : c2.vars ) {
216                                openVars[ var ] = c2.data;
217                        }
218                        env.push_back( std::move(c2) );
219                }
220        }
221
222        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ) {
223                env.print( out );
224                return out;
225        }
226
227        PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env ) {
228                for ( const EqvClass & c : env ) {
229                        maybeAccept( c.type, gc );
230                }
231                return gc;
232        }
233} // namespace ResolvExpr
234
235// Local Variables: //
236// tab-width: 4 //
237// mode: c++ //
238// compile-command: "make install" //
239// End: //
Note: See TracBrowser for help on using the repository browser.