| 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.h --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Aaron B. Moss
|
|---|
| 10 | // Created On : Sun May 17 12:24:58 2015
|
|---|
| 11 | // Last Modified By : Aaron B. Moss
|
|---|
| 12 | // Last Modified On : Wed Jun 13 16:31:00 2018
|
|---|
| 13 | // Update Count : 4
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <iostream> // for ostream
|
|---|
| 19 | #include <iterator>
|
|---|
| 20 | #include <list> // for list, list<>::iterator, list<>...
|
|---|
| 21 | #include <map> // for map, map<>::value_compare
|
|---|
| 22 | #include <set> // for set
|
|---|
| 23 | #include <string> // for string
|
|---|
| 24 | #include <utility> // for pair
|
|---|
| 25 | #include <vector> // for vector
|
|---|
| 26 |
|
|---|
| 27 | #include "Common/InternedString.h" // for interned_string
|
|---|
| 28 | #include "Common/PersistentDisjointSet.h" // for PersistentDisjointSet
|
|---|
| 29 | #include "Common/PersistentMap.h" // for PersistentMap
|
|---|
| 30 | #include "SynTree/Declaration.h" // for TypeDecl::Data, DeclarationWit...
|
|---|
| 31 | #include "SynTree/SynTree.h" // for UniqueId
|
|---|
| 32 | #include "SynTree/Type.h" // for Type, TypeInstType, Type::ForallList
|
|---|
| 33 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
|---|
| 34 |
|
|---|
| 35 | template< typename Pass > class PassVisitor;
|
|---|
| 36 | class GcTracer;
|
|---|
| 37 | namespace SymTab { class Indexer; }
|
|---|
| 38 |
|
|---|
| 39 | namespace ResolvExpr {
|
|---|
| 40 | // adding this comparison operator significantly improves assertion resolution run time for
|
|---|
| 41 | // some cases. The current resolution algorithm's speed partially depends on the order of
|
|---|
| 42 | // assertions. Assertions which have fewer possible matches should appear before
|
|---|
| 43 | // assertions which have more possible matches. This seems to imply that this could
|
|---|
| 44 | // be further improved by providing an indexer as an additional argument and ordering based
|
|---|
| 45 | // on the number of matches of the same kind (object, function) for the names of the
|
|---|
| 46 | // declarations.
|
|---|
| 47 | //
|
|---|
| 48 | // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this
|
|---|
| 49 | // comparator.
|
|---|
| 50 | //
|
|---|
| 51 | // Note: since this compares pointers for position, minor changes in the source file that affect
|
|---|
| 52 | // memory layout can alter compilation time in unpredictable ways. For example, the placement
|
|---|
| 53 | // of a line directive can reorder type pointers with respect to each other so that assertions
|
|---|
| 54 | // are seen in different orders, causing a potentially different number of unification calls
|
|---|
| 55 | // when resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering
|
|---|
| 56 | // line directives alone, so it would be nice to fix this comparison so that assertions compare
|
|---|
| 57 | // more consistently. I've tried to modify this to compare on mangle name instead of type as
|
|---|
| 58 | // the second comparator, but this causes some assertions to never be recorded. More
|
|---|
| 59 | // investigation is needed.
|
|---|
| 60 | struct AssertCompare {
|
|---|
| 61 | bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const {
|
|---|
| 62 | int cmp = d1->get_name().compare( d2->get_name() );
|
|---|
| 63 | return cmp < 0 ||
|
|---|
| 64 | ( cmp == 0 && d1->get_type() < d2->get_type() );
|
|---|
| 65 | }
|
|---|
| 66 | };
|
|---|
| 67 | struct AssertionSetValue {
|
|---|
| 68 | bool isUsed;
|
|---|
| 69 | // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID
|
|---|
| 70 | // of an assertion on the current type, with each successive ID being the ID of an
|
|---|
| 71 | // assertion pulled in by the previous ID. The last ID in the chain is the ID of the
|
|---|
| 72 | // assertion that pulled in the current assertion.
|
|---|
| 73 | std::list< UniqueId > idChain;
|
|---|
| 74 | };
|
|---|
| 75 | typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet;
|
|---|
| 76 | typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
|
|---|
| 77 |
|
|---|
| 78 | void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
|
|---|
| 79 | void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
|
|---|
| 80 |
|
|---|
| 81 | /// A data structure for holding all the necessary information for a type binding
|
|---|
| 82 | struct BoundType {
|
|---|
| 83 | Type* type;
|
|---|
| 84 | bool allowWidening;
|
|---|
| 85 | TypeDecl::Data data;
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 | #if 0
|
|---|
| 89 | /// An equivalence class, with its binding information
|
|---|
| 90 | struct EqvClass {
|
|---|
| 91 | std::set< std::string > vars;
|
|---|
| 92 | Type *type;
|
|---|
| 93 | bool allowWidening;
|
|---|
| 94 | TypeDecl::Data data;
|
|---|
| 95 |
|
|---|
| 96 | void initialize( const EqvClass &src, EqvClass &dest );
|
|---|
| 97 | EqvClass();
|
|---|
| 98 | EqvClass( std::vector<interned_string>&& vars, BoundType&& bound );
|
|---|
| 99 | EqvClass( const EqvClass &other );
|
|---|
| 100 | EqvClass &operator=( const EqvClass &other );
|
|---|
| 101 | void print( std::ostream &os, Indenter indent = {} ) const;
|
|---|
| 102 | };
|
|---|
| 103 | #endif
|
|---|
| 104 |
|
|---|
| 105 | class TypeEnvironment;
|
|---|
| 106 |
|
|---|
| 107 | /// A reference to an equivalence class that may be used to constitute one from its environment
|
|---|
| 108 | class ClassRef {
|
|---|
| 109 | friend TypeEnvironment;
|
|---|
| 110 |
|
|---|
| 111 | const TypeEnvironment* env; ///< Containing environment
|
|---|
| 112 | interned_string root; ///< Name of root type
|
|---|
| 113 |
|
|---|
| 114 | public:
|
|---|
| 115 | ClassRef() : env(nullptr), root(nullptr) {}
|
|---|
| 116 | ClassRef( const TypeEnvironment* env, interned_string root ) : env(env), root(root) {}
|
|---|
| 117 |
|
|---|
| 118 | /// Gets the root of the reference equivalence class;
|
|---|
| 119 | interned_string get_root() const { return root; }
|
|---|
| 120 |
|
|---|
| 121 | /// Ensures that root is still the representative element of this typeclass;
|
|---|
| 122 | /// undefined behaviour if called without referenced typeclass; returns new root
|
|---|
| 123 | inline interned_string update_root();
|
|---|
| 124 |
|
|---|
| 125 | /// Gets the type variables of the referenced equivalence class, empty list for none
|
|---|
| 126 | template<typename T = std::vector<interned_string>>
|
|---|
| 127 | inline T get_vars() const;
|
|---|
| 128 |
|
|---|
| 129 | /// Gets the bound type information of the referenced equivalence class, default if none
|
|---|
| 130 | inline BoundType get_bound() const;
|
|---|
| 131 |
|
|---|
| 132 | #if 0
|
|---|
| 133 | /// Gets the referenced equivalence class
|
|---|
| 134 | inline EqvClass get_class() const;
|
|---|
| 135 | EqvClass operator* () const { return get_class(); }
|
|---|
| 136 | #endif
|
|---|
| 137 |
|
|---|
| 138 | // Check that there is a referenced typeclass
|
|---|
| 139 | explicit operator bool() const { return env != nullptr; }
|
|---|
| 140 |
|
|---|
| 141 | bool operator== (const ClassRef& o) const { return env == o.env && root == o.root; }
|
|---|
| 142 | bool operator!= (const ClassRef& o) const { return !(*this == o); }
|
|---|
| 143 | };
|
|---|
| 144 |
|
|---|
| 145 | class TypeEnvironment {
|
|---|
| 146 | friend ClassRef;
|
|---|
| 147 |
|
|---|
| 148 | /// Backing storage for equivalence classes
|
|---|
| 149 | using Classes = PersistentDisjointSet<interned_string>;
|
|---|
| 150 | /// Type bindings included in this environment (from class root)
|
|---|
| 151 | using Bindings = PersistentMap<interned_string, BoundType>;
|
|---|
| 152 |
|
|---|
| 153 | /// Sets of equivalent type variables, stored by name
|
|---|
| 154 | Classes* classes;
|
|---|
| 155 | /// Bindings from roots of equivalence classes to type binding information.
|
|---|
| 156 | /// All roots have a binding so that the list of classes can be reconstituted, though these
|
|---|
| 157 | /// may be null.
|
|---|
| 158 | Bindings* bindings;
|
|---|
| 159 |
|
|---|
| 160 | /// Merges the classes rooted at root1 and root2, returning a pair containing the root and
|
|---|
| 161 | /// child of the bound class. Does not check for validity of merge.
|
|---|
| 162 | std::pair<interned_string, interned_string> mergeClasses(
|
|---|
| 163 | interned_string root1, interned_string root2 );
|
|---|
| 164 |
|
|---|
| 165 | public:
|
|---|
| 166 | class iterator : public std::iterator<
|
|---|
| 167 | std::forward_iterator_tag,
|
|---|
| 168 | ClassRef,
|
|---|
| 169 | std::iterator_traits<Bindings::iterator>::difference_type,
|
|---|
| 170 | ClassRef,
|
|---|
| 171 | ClassRef> {
|
|---|
| 172 | friend TypeEnvironment;
|
|---|
| 173 |
|
|---|
| 174 | const TypeEnvironment* env;
|
|---|
| 175 | Bindings::iterator it;
|
|---|
| 176 |
|
|---|
| 177 | iterator(const TypeEnvironment* e, Bindings::iterator&& i) : env(e), it(std::move(i)) {}
|
|---|
| 178 |
|
|---|
| 179 | ClassRef ref() const { return { env, it->first }; }
|
|---|
| 180 | public:
|
|---|
| 181 | iterator() = default;
|
|---|
| 182 |
|
|---|
| 183 | reference operator* () { return ref(); }
|
|---|
| 184 | pointer operator-> () { return ref(); }
|
|---|
| 185 |
|
|---|
| 186 | iterator& operator++ () { ++it; return *this; }
|
|---|
| 187 | iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
|
|---|
| 188 |
|
|---|
| 189 | bool operator== (const iterator& o) const { return env == o.env && it == o.it; }
|
|---|
| 190 | bool operator!= (const iterator& o) const { return !(*this == o); }
|
|---|
| 191 | };
|
|---|
| 192 |
|
|---|
| 193 | /// Finds a reference to the class containing `var`, invalid if none such.
|
|---|
| 194 | /// returned root variable will be valid regardless
|
|---|
| 195 | ClassRef lookup( interned_string var ) const;
|
|---|
| 196 | ClassRef lookup( const std::string &var ) const { return lookup( var ); }
|
|---|
| 197 |
|
|---|
| 198 | /// Binds a type variable to a type; returns false if fails
|
|---|
| 199 | bool bindVar( TypeInstType* typeInst, Type* bindTo, const TypeDecl::Data& data,
|
|---|
| 200 | AssertionSet& need, AssertionSet& have, const OpenVarSet& openVars,
|
|---|
| 201 | WidenMode widenMode, const SymTab::Indexer& indexer );
|
|---|
| 202 |
|
|---|
| 203 | /// Binds two type variables together; returns false if fails
|
|---|
| 204 | bool bindVarToVar( TypeInstType* var1, TypeInstType* var2, const TypeDecl::Data& data,
|
|---|
| 205 | AssertionSet& need, AssertionSet& have, const OpenVarSet& openVars,
|
|---|
| 206 | WidenMode widenMode, const SymTab::Indexer& indexer );
|
|---|
| 207 | #if !1
|
|---|
| 208 | void add( const EqvClass &eqvClass );
|
|---|
| 209 | void add( EqvClass &&eqvClass );
|
|---|
| 210 | void add( const Type::ForallList &tyDecls );
|
|---|
| 211 | void add( const TypeSubstitution & sub );
|
|---|
| 212 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
|
|---|
| 213 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
|
|---|
| 214 | void makeSubstitution( TypeSubstitution &result ) const;
|
|---|
| 215 | #endif
|
|---|
| 216 | bool isEmpty() const { return classes->empty(); }
|
|---|
| 217 | #if !1
|
|---|
| 218 | void print( std::ostream &os, Indenter indent = {} ) const;
|
|---|
| 219 | void simpleCombine( const TypeEnvironment &second );
|
|---|
| 220 | void extractOpenVars( OpenVarSet &openVars ) const;
|
|---|
| 221 | #endif
|
|---|
| 222 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
|
|---|
| 223 |
|
|---|
| 224 | #if !1
|
|---|
| 225 | /// Iteratively adds the environment of a new actual (with allowWidening = false),
|
|---|
| 226 | /// and extracts open variables.
|
|---|
| 227 | void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
|
|---|
| 228 | #endif
|
|---|
| 229 |
|
|---|
| 230 | iterator begin() { return { this, bindings->begin() }; }
|
|---|
| 231 | iterator end() { return { this, bindings->end() }; }
|
|---|
| 232 | #if 0
|
|---|
| 233 | typedef std::list< EqvClass >::const_iterator const_iterator;
|
|---|
| 234 | const_iterator begin() const { return env.begin(); }
|
|---|
| 235 | const_iterator end() const { return env.end(); }
|
|---|
| 236 | #endif
|
|---|
| 237 | };
|
|---|
| 238 |
|
|---|
| 239 | void ClassRef::update_root() { return root = env->classes->find( root ); }
|
|---|
| 240 |
|
|---|
| 241 | template<typename T>
|
|---|
| 242 | T ClassRef::get_vars() const {
|
|---|
| 243 | T vars;
|
|---|
| 244 | env->classes->find_class( root, std::inserter(vars, vars.end()) );
|
|---|
| 245 | return vars;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | BoundType ClassRef::get_bound() const {
|
|---|
| 249 | return env->bindings->get_or_default( root, BoundType{} );
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | #if 0
|
|---|
| 253 | EqvClass ClassRef::get_class() const { return { get_vars(), get_bound() }; }
|
|---|
| 254 | #endif
|
|---|
| 255 |
|
|---|
| 256 | template< typename SynTreeClass >
|
|---|
| 257 | int TypeEnvironment::apply( SynTreeClass *&type ) const {
|
|---|
| 258 | TypeSubstitution sub;
|
|---|
| 259 | makeSubstitution( sub );
|
|---|
| 260 | return sub.apply( type );
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | template< typename SynTreeClass >
|
|---|
| 264 | int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
|
|---|
| 265 | TypeSubstitution sub;
|
|---|
| 266 | makeSubstitution( sub );
|
|---|
| 267 | return sub.applyFree( type );
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | #if !1
|
|---|
| 271 | std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
|
|---|
| 272 | #endif
|
|---|
| 273 |
|
|---|
| 274 | PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env );
|
|---|
| 275 | } // namespace ResolvExpr
|
|---|
| 276 |
|
|---|
| 277 | // Local Variables: //
|
|---|
| 278 | // tab-width: 4 //
|
|---|
| 279 | // mode: c++ //
|
|---|
| 280 | // compile-command: "make install" //
|
|---|
| 281 | // End: //
|
|---|