source: src/ResolvExpr/TypeEnvironment.cc @ 68f9c43

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

First pass at delete removal

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