[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.h -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:24:58 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[6b0b624] | 12 | // Last Modified On : Sat Jul 22 09:35:45 2017 |
---|
| 13 | // Update Count : 3 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[ea6332d] | 18 | #include <iostream> // for ostream |
---|
| 19 | #include <list> // for list, list<>::iterator, list<>... |
---|
| 20 | #include <map> // for map, map<>::value_compare |
---|
| 21 | #include <set> // for set |
---|
| 22 | #include <string> // for string |
---|
[51b7345] | 23 | |
---|
[ea6332d] | 24 | #include "SynTree/Declaration.h" // for TypeDecl::Data, DeclarationWit... |
---|
| 25 | #include "SynTree/SynTree.h" // for UniqueId |
---|
| 26 | #include "SynTree/Type.h" // for Type, Type::ForallList |
---|
| 27 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
[51b7345] | 28 | |
---|
| 29 | namespace ResolvExpr { |
---|
[111a8af8] | 30 | // adding this comparison operator significantly improves assertion resolution run time for |
---|
| 31 | // some cases. The current resolution algorithm's speed partially depends on the order of |
---|
| 32 | // assertions. Assertions which have fewer possible matches should appear before |
---|
| 33 | // assertions which have more possible matches. This seems to imply that this could |
---|
| 34 | // be further improved by providing an indexer as an additional argument and ordering based |
---|
| 35 | // on the number of matches of the same kind (object, function) for the names of the |
---|
| 36 | // declarations. |
---|
| 37 | // |
---|
| 38 | // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this comparator. |
---|
[bd6e226] | 39 | // |
---|
| 40 | // Note: since this compares pointers for position, minor changes in the source file that affect |
---|
| 41 | // memory layout can alter compilation time in unpredictable ways. For example, the placement |
---|
| 42 | // of a line directive can reorder type pointers with respect to each other so that assertions |
---|
| 43 | // are seen in different orders, causing a potentially different number of unification calls when |
---|
| 44 | // resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering line directives |
---|
| 45 | // alone, so it would be nice to fix this comparison so that assertions compare more consistently. |
---|
| 46 | // I've tried to modify this to compare on mangle name instead of type as the second comparator, but |
---|
| 47 | // this causes some assertions to never be recorded. More investigation is needed. |
---|
[03da511] | 48 | struct AssertCompare { |
---|
[111a8af8] | 49 | bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const { |
---|
| 50 | int cmp = d1->get_name().compare( d2->get_name() ); |
---|
| 51 | return cmp < 0 || |
---|
| 52 | ( cmp == 0 && d1->get_type() < d2->get_type() ); |
---|
| 53 | } |
---|
[03da511] | 54 | }; |
---|
[6c3a988f] | 55 | struct AssertionSetValue { |
---|
| 56 | bool isUsed; |
---|
| 57 | // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID of an assertion on the current type, |
---|
| 58 | // with each successive ID being the ID of an assertion pulled in by the previous ID. The last ID in the chain is |
---|
| 59 | // the ID of the assertion that pulled in the current assertion. |
---|
| 60 | std::list< UniqueId > idChain; |
---|
| 61 | }; |
---|
| 62 | typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet; |
---|
[2c57025] | 63 | typedef std::map< std::string, TypeDecl::Data > OpenVarSet; |
---|
[51b7345] | 64 | |
---|
[a32b204] | 65 | void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 ); |
---|
| 66 | void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 ); |
---|
[51b7345] | 67 | |
---|
[a32b204] | 68 | struct EqvClass { |
---|
| 69 | std::set< std::string > vars; |
---|
| 70 | Type *type; |
---|
| 71 | bool allowWidening; |
---|
[2c57025] | 72 | TypeDecl::Data data; |
---|
[03da511] | 73 | |
---|
[a32b204] | 74 | void initialize( const EqvClass &src, EqvClass &dest ); |
---|
[00ac42e] | 75 | void initialize( const EqvClass &src, EqvClass &dest, const Type *ty ); |
---|
[a32b204] | 76 | EqvClass(); |
---|
| 77 | EqvClass( const EqvClass &other ); |
---|
[00ac42e] | 78 | EqvClass( const EqvClass &other, const Type *ty ); |
---|
[a32b204] | 79 | EqvClass &operator=( const EqvClass &other ); |
---|
| 80 | ~EqvClass(); |
---|
[50377a4] | 81 | void print( std::ostream &os, Indenter indent = {} ) const; |
---|
[a32b204] | 82 | }; |
---|
[51b7345] | 83 | |
---|
[a32b204] | 84 | class TypeEnvironment { |
---|
| 85 | public: |
---|
[00ac42e] | 86 | const EqvClass* lookup( const std::string &var ) const; |
---|
[a32b204] | 87 | void add( const EqvClass &eqvClass ); |
---|
[00ac42e] | 88 | void add( EqvClass &&eqvClass ); |
---|
[8c49c0e] | 89 | void add( const Type::ForallList &tyDecls ); |
---|
[09c72d5] | 90 | void add( const TypeSubstitution & sub ); |
---|
[a32b204] | 91 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const; |
---|
| 92 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const; |
---|
| 93 | void makeSubstitution( TypeSubstitution &result ) const; |
---|
| 94 | bool isEmpty() const { return env.empty(); } |
---|
[50377a4] | 95 | void print( std::ostream &os, Indenter indent = {} ) const; |
---|
[a32b204] | 96 | void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ); |
---|
| 97 | void simpleCombine( const TypeEnvironment &second ); |
---|
| 98 | void extractOpenVars( OpenVarSet &openVars ) const; |
---|
| 99 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); } |
---|
[03da511] | 100 | |
---|
[98a249fb] | 101 | /// Iteratively adds the environment of a new actual (with allowWidening = false), |
---|
[a585396] | 102 | /// and extracts open variables. |
---|
| 103 | void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars ); |
---|
| 104 | |
---|
[a32b204] | 105 | typedef std::list< EqvClass >::iterator iterator; |
---|
| 106 | iterator begin() { return env.begin(); } |
---|
| 107 | iterator end() { return env.end(); } |
---|
| 108 | typedef std::list< EqvClass >::const_iterator const_iterator; |
---|
| 109 | const_iterator begin() const { return env.begin(); } |
---|
| 110 | const_iterator end() const { return env.end(); } |
---|
| 111 | private: |
---|
| 112 | std::list< EqvClass > env; |
---|
| 113 | std::list< EqvClass >::iterator internal_lookup( const std::string &var ); |
---|
| 114 | }; |
---|
[51b7345] | 115 | |
---|
[a32b204] | 116 | template< typename SynTreeClass > |
---|
| 117 | int TypeEnvironment::apply( SynTreeClass *&type ) const { |
---|
| 118 | TypeSubstitution sub; |
---|
| 119 | makeSubstitution( sub ); |
---|
| 120 | return sub.apply( type ); |
---|
| 121 | } |
---|
[51b7345] | 122 | |
---|
[a32b204] | 123 | template< typename SynTreeClass > |
---|
| 124 | int TypeEnvironment::applyFree( SynTreeClass *&type ) const { |
---|
| 125 | TypeSubstitution sub; |
---|
| 126 | makeSubstitution( sub ); |
---|
| 127 | return sub.applyFree( type ); |
---|
| 128 | } |
---|
[98a249fb] | 129 | |
---|
| 130 | std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env ); |
---|
[51b7345] | 131 | } // namespace ResolvExpr |
---|
| 132 | |
---|
[a32b204] | 133 | // Local Variables: // |
---|
| 134 | // tab-width: 4 // |
---|
| 135 | // mode: c++ // |
---|
| 136 | // compile-command: "make install" // |
---|
| 137 | // End: // |
---|