source: src/AST/TypeEnvironment.cpp @ 7aa209e7

Last change on this file since 7aa209e7 was 24d6572, checked in by Fangren Yu <f37yu@…>, 12 months ago

Merge branch 'master' into ast-experimental

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