source: src/ResolvExpr/TypeEnvironment.cc @ b9dae14c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b9dae14c was cd6a6ff, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Improved coverage of deterministic_output to be much finer grain.

  • Property mode set to 100644
File size: 16.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 : Andrew Beach
12// Last Modified On : Fri Jun 18 14:27:00 2019
13// Update Count     : 5
14//
15
16#include <cassert>                     // for assert
17#include <algorithm>                   // for copy, set_intersection
18#include <iterator>                    // for ostream_iterator, insert_iterator
19#include <memory>                      // for unique_ptr
20#include <utility>                     // for pair, move
21
22#include "CompilationState.h"          // for deterministic_output
23#include "Common/utility.h"            // for maybeClone
24#include "SynTree/Type.h"              // for Type, FunctionType, Type::Fora...
25#include "SynTree/TypeSubstitution.h"  // for TypeSubstitution
26#include "Tuples/Tuples.h"             // for isTtype
27#include "TypeEnvironment.h"
28#include "typeops.h"                   // for occurs
29#include "Unify.h"                     // for unifyInexact
30
31namespace ResolvExpr {
32        void printAssertionSet( const AssertionSet &assertions, std::ostream &os, int indent ) {
33                for ( AssertionSet::const_iterator i = assertions.begin(); i != assertions.end(); ++i ) {
34                        i->first->print( os, indent );
35                        if ( i->second.isUsed ) {
36                                os << "(used)";
37                        } else {
38                                os << "(not used)";
39                        } // if
40                } // for
41        }
42
43        void printOpenVarSet( const OpenVarSet &openVars, std::ostream &os, int indent ) {
44                os << std::string( indent, ' ' );
45                for ( OpenVarSet::const_iterator i = openVars.begin(); i != openVars.end(); ++i ) {
46                        os << i->first << "(" << i->second << ") ";
47                } // for
48        }
49
50        void EqvClass::initialize( const EqvClass &src, EqvClass &dest ) {
51                initialize( src, dest, src.type );
52        }
53
54        void EqvClass::initialize( const EqvClass &src, EqvClass &dest, const Type *ty ) {
55                dest.vars = src.vars;
56                dest.type = maybeClone( ty );
57                dest.allowWidening = src.allowWidening;
58                dest.data = src.data;
59        }
60
61        EqvClass::EqvClass() : type( nullptr ), allowWidening( true ) {
62        }
63
64        EqvClass::EqvClass( const EqvClass &other ) {
65                initialize( other, *this );
66        }
67
68        EqvClass::EqvClass( const EqvClass &other, const Type *ty ) {
69                initialize( other, *this, ty );
70        }
71
72        EqvClass::EqvClass( EqvClass &&other )
73        : vars{std::move(other.vars)}, type{other.type},
74          allowWidening{std::move(other.allowWidening)}, data{std::move(other.data)} {
75                  other.type = nullptr;
76        }
77
78        EqvClass &EqvClass::operator=( const EqvClass &other ) {
79                if ( this == &other ) return *this;
80                delete type;
81                initialize( other, *this );
82                return *this;
83        }
84
85        EqvClass &EqvClass::operator=( EqvClass &&other ) {
86                if ( this == &other ) return *this;
87                delete type;
88
89                vars = std::move(other.vars);
90                type = other.type;
91                other.type = nullptr;
92                allowWidening = std::move(other.allowWidening);
93                data = std::move(other.data);
94
95                return *this;
96        }
97
98        EqvClass::~EqvClass() {
99                delete type;
100        }
101
102        void EqvClass::set_type( Type* ty ) {
103                if ( ty == type ) return;
104                delete type;
105                type = ty;
106        }
107
108        void EqvClass::print( std::ostream &os, Indenter indent ) const {
109                os << "(";
110                bool first = true;
111                for(const auto & var : vars) {
112                        if(first) first = false;
113                        else os << " ";
114                        if( deterministic_output && isUnboundType(var) ) os << "[unbound]";
115                        else os << var;
116                }
117                os << ")";
118                if ( type ) {
119                        os << " -> ";
120                        type->print( os, indent+1 );
121                } // if
122                if ( ! allowWidening ) {
123                        os << " (no widening)";
124                } // if
125                os << std::endl;
126        }
127
128        const EqvClass* TypeEnvironment::lookup( const std::string &var ) const {
129                for ( ClassList::const_iterator i = env.begin(); i != env.end(); ++i ) {
130                        if ( i->vars.find( var ) != i->vars.end() ) return &*i;
131                } // for
132                return nullptr;
133        }
134
135        /// Removes any class from env that intersects eqvClass
136        void filterOverlappingClasses( std::list<EqvClass> &env, const EqvClass &eqvClass ) {
137                for ( auto i = env.begin(); i != env.end(); ) {
138                        auto next = i;
139                        ++next;
140                        std::set<std::string> intersection;
141                        std::set_intersection( i->vars.begin(), i->vars.end(), eqvClass.vars.begin(), eqvClass.vars.end(),
142                                std::inserter( intersection, intersection.begin() ) );
143                        if ( ! intersection.empty() ) { env.erase( i ); }
144                        i = next;
145                }
146        }
147
148        void TypeEnvironment::add( EqvClass &&eqvClass ) {
149                filterOverlappingClasses( env, eqvClass );
150                env.push_back( std::move(eqvClass) );
151        }
152
153        void TypeEnvironment::add( const Type::ForallList &tyDecls ) {
154                for ( Type::ForallList::const_iterator i = tyDecls.begin(); i != tyDecls.end(); ++i ) {
155                        EqvClass newClass;
156                        newClass.vars.insert( (*i)->get_name() );
157                        newClass.data = TypeDecl::Data{ (*i) };
158                        env.push_back( std::move(newClass) );
159                } // for
160        }
161
162        void TypeEnvironment::add( const TypeSubstitution & sub ) {
163                EqvClass newClass;
164                for ( auto p : sub ) {
165                        newClass.vars.insert( p.first );
166                        newClass.type = p.second->clone();
167                        newClass.allowWidening = false;
168                        // Minimal assumptions. Not technically correct, but might be good enough, and
169                        // is the best we can do at the moment since information is lost in the
170                        // transition to TypeSubstitution
171                        newClass.data = TypeDecl::Data{ TypeDecl::Dtype, false };
172                        add( std::move(newClass) );
173                }
174        }
175
176        void TypeEnvironment::makeSubstitution( TypeSubstitution &sub ) const {
177                for ( ClassList::const_iterator theClass = env.begin(); theClass != env.end(); ++theClass ) {
178                        for ( std::set< std::string >::const_iterator theVar = theClass->vars.begin(); theVar != theClass->vars.end(); ++theVar ) {
179                                if ( theClass->type ) {
180                                        sub.add( *theVar, theClass->type );
181                                } else if ( theVar != theClass->vars.begin() ) {
182                                        TypeInstType *newTypeInst = new TypeInstType( Type::Qualifiers(), *theClass->vars.begin(), theClass->data.kind == TypeDecl::Ftype );
183                                        sub.add( *theVar, newTypeInst );
184                                        delete newTypeInst;
185                                } // if
186                        } // for
187                } // for
188                sub.normalize();
189        }
190
191        void TypeEnvironment::print( std::ostream &os, Indenter indent ) const {
192                for ( const EqvClass & theClass : env ) {
193                        theClass.print( os, indent );
194                } // for
195        }
196
197        TypeEnvironment::ClassList::iterator TypeEnvironment::internal_lookup( const std::string &var ) {
198                for ( ClassList::iterator i = env.begin(); i != env.end(); ++i ) {
199                        if ( i->vars.count( var ) ) return i;
200                } // for
201                return env.end();
202        }
203
204        void TypeEnvironment::simpleCombine( const TypeEnvironment &second ) {
205                env.insert( env.end(), second.env.begin(), second.env.end() );
206        }
207
208        // xxx -- this should maybe be worrying about iterator invalidation (see resolv-proto)
209        bool TypeEnvironment::mergeBound( EqvClass& to, const EqvClass& from, OpenVarSet& openVars, const SymTab::Indexer& indexer ) {
210                if ( from.type ) {
211                        if ( to.type ) {
212                                // attempt to unify bound types
213                                std::unique_ptr<Type> toType{ to.type->clone() }, fromType{ from.type->clone() };
214                                WidenMode widen{ to.allowWidening, from.allowWidening };
215                                Type* common = nullptr;
216                                AssertionSet need, have;
217                                if ( unifyInexact( toType.get(), fromType.get(), *this, need, have, openVars, widen, indexer, common ) ) {
218                                        // unifies, set common type if necessary
219                                        if ( common ) {
220                                                common->get_qualifiers() = Type::Qualifiers{};
221                                                to.set_type( common );
222                                        }
223                                } else return false; // cannot unify
224                        } else {
225                                to.type = from.type->clone();
226                        }
227                }
228
229                // unify widening if matches
230                to.allowWidening &= from.allowWidening;
231                return true;
232        }
233
234        // xxx -- this should maybe be worrying about iterator invalidation (see resolv-proto)
235        bool TypeEnvironment::mergeClasses( TypeEnvironment::ClassList::iterator to, TypeEnvironment::ClassList::iterator from, OpenVarSet& openVars, const SymTab::Indexer& indexer ) {
236                EqvClass& r = *to;
237                EqvClass& s = *from;
238
239                // ensure bounds match
240                if ( ! mergeBound( r, s, openVars, indexer ) ) return false;
241
242                // check safely bindable
243                if ( r.type && occursIn( r.type, s.vars.begin(), s.vars.end(), *this ) ) return false;
244
245                // merge classes in
246                r.vars.insert( s.vars.begin(), s.vars.end() );
247                r.allowWidening &= s.allowWidening;
248                env.erase( from );
249
250                return true;
251        }
252
253        bool TypeEnvironment::combine( const TypeEnvironment& o, OpenVarSet& openVars, const SymTab::Indexer& indexer ) {
254                // short-circuit easy cases
255                if ( o.isEmpty() ) return true;
256                if ( isEmpty() ) {
257                        simpleCombine( o );
258                        return true;
259                }
260
261                // merge classes
262                for ( auto ct = o.env.begin(); ct != o.env.end(); ++ct ) {
263                        const EqvClass& c = *ct;
264
265                        // typeclass in local environment bound to c
266                        auto rt = env.end();
267
268                        // look for first existing bound variable
269                        auto vt = c.vars.begin();
270                        for ( ; vt != c.vars.end(); ++vt ) {
271                                rt = internal_lookup( *vt );
272                                if ( rt != env.end() ) break;
273                        }
274
275                        if ( rt != env.end() ) {  // c needs to be merged into *rt
276                                EqvClass& r = *rt;
277                                // merge bindings
278                                if ( ! mergeBound( r, c, openVars, indexer ) ) return false;
279                                // merge previous unbound variables into this class, checking occurs if needed
280                                if ( r.type ) for ( auto ut = c.vars.begin(); ut != vt; ++ut ) {
281                                        if ( occurs( r.type, *ut, *this ) ) return false;
282                                        r.vars.insert( *ut );
283                                } else { r.vars.insert( c.vars.begin(), vt ); }
284                                // merge subsequent variables into this class (skipping *vt, already there)
285                                while ( ++vt != c.vars.end() ) {
286                                        auto st = internal_lookup( *vt );
287                                        if ( st == env.end() ) {
288                                                // unbound, safe to add if passes occurs
289                                                if ( r.type && occurs( r.type, *vt, *this ) ) return false;
290                                                r.vars.insert( *vt );
291                                        } else if ( st != rt ) {
292                                                // bound, but not to the same class
293                                                if ( ! mergeClasses( rt, st, openVars, indexer ) ) return false;
294                                        }   // ignore bound into the same class
295                                }
296                        } else {  // no variables in c bound; just copy up
297                                env.push_back( c );
298                        }
299                }
300
301                // merged all classes
302                return true;
303        }
304
305        void TypeEnvironment::extractOpenVars( OpenVarSet &openVars ) const {
306                for ( ClassList::const_iterator eqvClass = env.begin(); eqvClass != env.end(); ++eqvClass ) {
307                        for ( std::set< std::string >::const_iterator var = eqvClass->vars.begin(); var != eqvClass->vars.end(); ++var ) {
308                                openVars[ *var ] = eqvClass->data;
309                        } // for
310                } // for
311        }
312
313        void TypeEnvironment::addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars ) {
314                for ( const EqvClass& c : actualEnv ) {
315                        EqvClass c2 = c;
316                        c2.allowWidening = false;
317                        for ( const std::string& var : c2.vars ) {
318                                openVars[ var ] = c2.data;
319                        }
320                        env.push_back( std::move(c2) );
321                }
322        }
323
324        bool isFtype( const Type * type ) {
325                if ( dynamic_cast< const FunctionType * >( type ) ) {
326                        return true;
327                } else if ( const TypeInstType *typeInst = dynamic_cast< const TypeInstType * >( type ) ) {
328                        return typeInst->get_isFtype();
329                } // if
330                return false;
331        }
332
333        bool tyVarCompatible( const TypeDecl::Data & data, const Type * type ) {
334                switch ( data.kind ) {
335                  case TypeDecl::Dtype:
336                        // to bind to an object type variable, the type must not be a function type.
337                        // if the type variable is specified to be a complete type then the incoming
338                        // type must also be complete
339                        // xxx - should this also check that type is not a tuple type and that it's not a ttype?
340                        return ! isFtype( type ) && (! data.isComplete || type->isComplete() );
341                  case TypeDecl::Ftype:
342                        return isFtype( type );
343                  case TypeDecl::Ttype:
344                        // ttype unifies with any tuple type
345                        return dynamic_cast< const TupleType * >( type ) || Tuples::isTtype( type );
346                  default:
347                        assertf(false, "Unhandled tyvar kind: %d", data.kind);
348                } // switch
349                return false;
350        }
351
352        bool TypeEnvironment::bindVar( const TypeInstType *typeInst, Type *bindTo, const TypeDecl::Data & data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer ) {
353
354                // remove references from other, so that type variables can only bind to value types
355                bindTo = bindTo->stripReferences();
356                OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
357                assert( tyvar != openVars.end() );
358                if ( ! tyVarCompatible( tyvar->second, bindTo ) ) {
359                        return false;
360                } // if
361                if ( occurs( bindTo, typeInst->get_name(), *this ) ) {
362                        return false;
363                } // if
364                auto curClass = internal_lookup( typeInst->get_name() );
365                if ( curClass != env.end() ) {
366                        if ( curClass->type ) {
367                                Type *common = 0;
368                                // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
369                                std::unique_ptr< Type > newType( curClass->type->clone() );
370                                newType->tq = typeInst->tq;
371                                if ( unifyInexact( newType.get(), bindTo, *this, need, have, openVars, widen & WidenMode( curClass->allowWidening, true ), indexer, common ) ) {
372                                        if ( common ) {
373                                                common->get_qualifiers() = Type::Qualifiers{};
374                                                curClass->set_type( common );
375                                        } // if
376                                } else return false;
377                        } else {
378                                Type* newType = bindTo->clone();
379                                newType->get_qualifiers() = Type::Qualifiers{};
380                                curClass->set_type( newType );
381                                curClass->allowWidening = widen.first && widen.second;
382                        } // if
383                } else {
384                        EqvClass newClass;
385                        newClass.vars.insert( typeInst->get_name() );
386                        newClass.type = bindTo->clone();
387                        newClass.type->get_qualifiers() = Type::Qualifiers();
388                        newClass.allowWidening = widen.first && widen.second;
389                        newClass.data = data;
390                        env.push_back( std::move(newClass) );
391                } // if
392                return true;
393        }
394
395        bool TypeEnvironment::bindVarToVar( const TypeInstType * var1, const TypeInstType * var2,
396                        TypeDecl::Data && data, AssertionSet &need, AssertionSet &have,
397                        const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer ) {
398
399                auto class1 = internal_lookup( var1->get_name() );
400                auto class2 = internal_lookup( var2->get_name() );
401
402                // exit early if variables already bound together
403                if ( class1 != env.end() && class1 == class2 ) {
404                        class1->allowWidening &= widen;
405                        return true;
406                }
407
408                bool widen1 = false, widen2 = false;
409                const Type *type1 = nullptr, *type2 = nullptr;
410
411                // check for existing bindings, perform occurs check
412                if ( class1 != env.end() ) {
413                        if ( class1->type ) {
414                                if ( occurs( class1->type, var2->get_name(), *this ) ) return false;
415                                type1 = class1->type;
416                        } // if
417                        widen1 = widen.first && class1->allowWidening;
418                } // if
419                if ( class2 != env.end() ) {
420                        if ( class2->type ) {
421                                if ( occurs( class2->type, var1->get_name(), *this ) ) return false;
422                                type2 = class2->type;
423                        } // if
424                        widen2 = widen.second && class2->allowWidening;
425                } // if
426
427                if ( type1 && type2 ) {
428                        // both classes bound, merge if bound types can be unified
429                        std::unique_ptr<Type> newType1{ type1->clone() }, newType2{ type2->clone() };
430                        WidenMode newWidenMode{ widen1, widen2 };
431                        Type *common = 0;
432                        if ( unifyInexact( newType1.get(), newType2.get(), *this, need, have, openVars, newWidenMode, indexer, common ) ) {
433                                class1->vars.insert( class2->vars.begin(), class2->vars.end() );
434                                class1->allowWidening = widen1 && widen2;
435                                if ( common ) {
436                                        common->get_qualifiers() = Type::Qualifiers{};
437                                        class1->set_type( common );
438                                }
439                                class1->data.isComplete |= data.isComplete;
440                                env.erase( class2 );
441                        } else return false;
442                } else if ( class1 != env.end() && class2 != env.end() ) {
443                        // both classes exist, at least one unbound, merge unconditionally
444                        if ( type1 ) {
445                                class1->vars.insert( class2->vars.begin(), class2->vars.end() );
446                                class1->allowWidening = widen1;
447                                class1->data.isComplete |= data.isComplete;
448                                env.erase( class2 );
449                        } else {
450                                class2->vars.insert( class1->vars.begin(), class1->vars.end() );
451                                class2->allowWidening = widen2;
452                                class2->data.isComplete |= data.isComplete;
453                                env.erase( class1 );
454                        } // if
455                } else if ( class1 != env.end() ) {
456                        // var2 unbound, add to class1
457                        class1->vars.insert( var2->get_name() );
458                        class1->allowWidening = widen1;
459                        class1->data.isComplete |= data.isComplete;
460                } else if ( class2 != env.end() ) {
461                        // var1 unbound, add to class2
462                        class2->vars.insert( var1->get_name() );
463                        class2->allowWidening = widen2;
464                        class2->data.isComplete |= data.isComplete;
465                } else {
466                        // neither var bound, create new class
467                        EqvClass newClass;
468                        newClass.vars.insert( var1->get_name() );
469                        newClass.vars.insert( var2->get_name() );
470                        newClass.allowWidening = widen1 && widen2;
471                        newClass.data = data;
472                        env.push_back( std::move(newClass) );
473                } // if
474                return true;
475        }
476
477        void TypeEnvironment::forbidWidening() {
478                for ( EqvClass& c : env ) c.allowWidening = false;
479        }
480
481        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ) {
482                env.print( out );
483                return out;
484        }
485} // namespace ResolvExpr
486
487// Local Variables: //
488// tab-width: 4 //
489// mode: c++ //
490// compile-command: "make install" //
491// End: //
Note: See TracBrowser for help on using the repository browser.