Changeset b067d9b for src/ResolvExpr/TypeEnvironment.h
- Timestamp:
- Oct 29, 2019, 4:01:24 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 773db65, 9421f3d8
- Parents:
- 7951100 (diff), 8364209 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/TypeEnvironment.h
r7951100 rb067d9b 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:24:58 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat Jul 22 09:35:45 201713 // Update Count : 311 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Jul 19 17:00:10 2019 13 // Update Count : 10 14 14 // 15 15 … … 18 18 #include <iostream> // for ostream 19 19 #include <list> // for list, list<>::iterator, list<>... 20 #include <map> // for map, map<>::value_compare 21 #include <set> // for set 20 #include <map> // for map, map<>::value_compare 21 #include <unordered_map> 22 #include <set> // for set 22 23 #include <string> // for string 24 #include <utility> // for move, swap 25 26 #include "WidenMode.h" // for WidenMode 23 27 24 28 #include "SynTree/Declaration.h" // for TypeDecl::Data, DeclarationWit... … … 36 40 // declarations. 37 41 // 38 // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this comparator. 42 // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this 43 // comparator. 39 44 // 40 45 // Note: since this compares pointers for position, minor changes in the source file that affect 41 46 // memory layout can alter compilation time in unpredictable ways. For example, the placement 42 47 // 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. 48 // are seen in different orders, causing a potentially different number of unification calls 49 // when resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering 50 // line directives alone, so it would be nice to fix this comparison so that assertions compare 51 // more consistently. I've tried to modify this to compare on mangle name instead of type as 52 // the second comparator, but this causes some assertions to never be recorded. More 53 // investigation is needed. 48 54 struct AssertCompare { 49 bool operator()( DeclarationWithType * d1,DeclarationWithType * d2 ) const {55 bool operator()( const DeclarationWithType * d1, const DeclarationWithType * d2 ) const { 50 56 int cmp = d1->get_name().compare( d2->get_name() ); 51 57 return cmp < 0 || … … 54 60 }; 55 61 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; 62 bool isUsed; ///< True if assertion needs to be resolved 63 UniqueId resnSlot; ///< ID of slot assertion belongs to 64 65 AssertionSetValue() : isUsed(false), resnSlot(0) {} 61 66 }; 62 typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet; 63 typedef std::map< std::string, TypeDecl::Data > OpenVarSet; 67 typedef std::map< const DeclarationWithType *, AssertionSetValue, AssertCompare > AssertionSet; 68 typedef std::unordered_map< std::string, TypeDecl::Data > OpenVarSet; 69 70 /// merges one set of open vars into another 71 static inline void mergeOpenVars( OpenVarSet& dst, const OpenVarSet& src ) { 72 for ( const auto& entry : src ) { dst[ entry.first ] = entry.second; } 73 } 64 74 65 75 void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 ); … … 68 78 struct EqvClass { 69 79 std::set< std::string > vars; 70 Type * type;80 Type * type; 71 81 bool allowWidening; 72 82 TypeDecl::Data data; … … 77 87 EqvClass( const EqvClass &other ); 78 88 EqvClass( const EqvClass &other, const Type *ty ); 89 EqvClass( EqvClass &&other ); 79 90 EqvClass &operator=( const EqvClass &other ); 91 EqvClass &operator=( EqvClass &&other ); 80 92 ~EqvClass(); 81 93 void print( std::ostream &os, Indenter indent = {} ) const; 94 95 /// Takes ownership of `ty`, freeing old `type` 96 void set_type(Type* ty); 82 97 }; 83 98 84 99 class TypeEnvironment { 100 using ClassList = std::list< EqvClass >; 85 101 public: 86 102 const EqvClass* lookup( const std::string &var ) const; 87 void add( const EqvClass &eqvClass );103 private: 88 104 void add( EqvClass &&eqvClass ); 105 public: 89 106 void add( const Type::ForallList &tyDecls ); 90 107 void add( const TypeSubstitution & sub ); … … 94 111 bool isEmpty() const { return env.empty(); } 95 112 void print( std::ostream &os, Indenter indent = {} ) const; 96 void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) ); 113 114 /// Simply concatenate the second environment onto this one; no safety checks performed 97 115 void simpleCombine( const TypeEnvironment &second ); 116 117 private: 118 /// Unifies the type bound of to with the type bound of from, returning false if fails 119 bool mergeBound( EqvClass& to, const EqvClass& from, OpenVarSet& openVars, const SymTab::Indexer& indexer ); 120 121 /// Merges two type classes from local environment, returning false if fails 122 bool mergeClasses( ClassList::iterator to, ClassList::iterator from, OpenVarSet& openVars, const SymTab::Indexer& indexer ); 123 124 public: 125 /// Merges the second environment with this one, checking compatibility. 126 /// Returns false if fails, but does NOT roll back partial changes. 127 bool combine( const TypeEnvironment& second, OpenVarSet& openVars, const SymTab::Indexer& indexer ); 128 98 129 void extractOpenVars( OpenVarSet &openVars ) const; 99 130 TypeEnvironment *clone() const { return new TypeEnvironment( *this ); } … … 103 134 void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars ); 104 135 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(); } 136 /// Binds the type class represented by `typeInst` to the type `bindTo`; will add 137 /// the class if needed. Returns false on failure. 138 bool bindVar( const TypeInstType * typeInst, Type * bindTo, const TypeDecl::Data & data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer ); 139 140 /// Binds the type classes represented by `var1` and `var2` together; will add 141 /// one or both classes if needed. Returns false on failure. 142 bool bindVarToVar( const TypeInstType * var1, const TypeInstType * var2, TypeDecl::Data && data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer ); 143 144 /// Disallows widening for all bindings in the environment 145 void forbidWidening(); 146 147 using iterator = ClassList::const_iterator; 148 iterator begin() const { return env.begin(); } 149 iterator end() const { return env.end(); } 150 111 151 private: 112 std::list< EqvClass > env; 113 std::list< EqvClass >::iterator internal_lookup( const std::string &var ); 152 ClassList env; 153 154 ClassList::iterator internal_lookup( const std::string &var ); 114 155 }; 115 156
Note:
See TracChangeset
for help on using the changeset viewer.