source: src/ResolvExpr/TypeEnvironment.cc @ 4e7cc5ce

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 4e7cc5ce was 4e7cc5ce, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Minor cleanup

  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[a32b204]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//
[03da511]7// TypeEnvironment.cc --
[a32b204]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//
[51b7345]15
[ea6332d]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
[51b7345]20
[ea6332d]21#include "Common/utility.h"            // for maybeClone
22#include "SynTree/Type.h"              // for Type, FunctionType, Type::Fora...
23#include "SynTree/TypeSubstitution.h"  // for TypeSubstitution
[51b7345]24#include "TypeEnvironment.h"
25
26namespace ResolvExpr {
[a32b204]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 );
[6c3a988f]30                        if ( i->second.isUsed ) {
[a32b204]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;
[2c57025]49                dest.data = src.data;
[a32b204]50        }
51
[4e7cc5ce]52        EqvClass::EqvClass() : type( nullptr ), allowWidening( true ) {
[a32b204]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
[50377a4]70        void EqvClass::print( std::ostream &os, Indenter indent ) const {
71                os << "( ";
[a32b204]72                std::copy( vars.begin(), vars.end(), std::ostream_iterator< std::string >( os, " " ) );
73                os << ")";
74                if ( type ) {
75                        os << " -> ";
[50377a4]76                        type->print( os, indent+1 );
[a32b204]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() ) {
[51b7345]87///       std::cout << var << " is in class ";
88///       i->print( std::cout );
[a32b204]89                                eqvClass = *i;
90                                return true;
91                        }
[51b7345]92///     std::cout << var << " is not in class ";
93///     i->print( std::cout );
[a32b204]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
[8c49c0e]113        void TypeEnvironment::add( const Type::ForallList &tyDecls ) {
114                for ( Type::ForallList::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) {
[a32b204]115                        EqvClass newClass;
116                        newClass.vars.insert( (*i)->get_name() );
[2c57025]117                        newClass.data = TypeDecl::Data{ (*i) };
[a32b204]118                        env.push_back( newClass );
119                } // for
120        }
121
[09c72d5]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
[a32b204]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 ) {
[8b11840]139///       std::cerr << "adding " << *theVar;
[a32b204]140                                if ( theClass->type ) {
[8b11840]141///         std::cerr << " bound to ";
142///         theClass->type->print( std::cerr );
143///         std::cerr << std::endl;
[a32b204]144                                        sub.add( *theVar, theClass->type );
145                                } else if ( theVar != theClass->vars.begin() ) {
[2c57025]146                                        TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->data.kind == TypeDecl::Ftype );
[8b11840]147///         std::cerr << " bound to variable " << *theClass->vars.begin() << std::endl;
[a32b204]148                                        sub.add( *theVar, newTypeInst );
149                                        delete newTypeInst;
150                                } // if
151                        } // for
152                } // for
[51b7345]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 );
[a32b204]157                sub.normalize();
158        }
159
[50377a4]160        void TypeEnvironment::print( std::ostream &os, Indenter indent ) const {
[4e7cc5ce]161                for ( const EqvClass & theClass : env ) {
162                        theClass.print( os, indent );
[a32b204]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 ) {
[2c57025]212                                openVars[ *var ] = eqvClass->data;
[a32b204]213                        } // for
214                } // for
215        }
[51b7345]216
[a585396]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
[98a249fb]228        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ) {
229                env.print( out );
230                return out;
231        }
[51b7345]232} // namespace ResolvExpr
[a32b204]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.