source: src/ResolvExpr/TypeEnvironment.h @ 6d6e829

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 6d6e829 was 6d6e829, checked in by Aaron Moss <a3moss@…>, 6 years ago

First compiling draft of deferred assertions (build failure)

  • Property mode set to 100644
File size: 7.6 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.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 12:24:58 2015
11// Last Modified By : Aaron B. Moss
12// Last Modified On : Mon Jun 18 11:58:00 2018
13// Update Count     : 4
14//
15
16#pragma once
17
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
23#include <utility>                     // for move, swap
24
25#include "WidenMode.h"                 // for WidenMode
26
27#include "SynTree/Declaration.h"       // for TypeDecl::Data, DeclarationWit...
28#include "SynTree/SynTree.h"           // for UniqueId
29#include "SynTree/Type.h"              // for Type, Type::ForallList
30#include "SynTree/TypeSubstitution.h"  // for TypeSubstitution
31
32namespace ResolvExpr {
33        // adding this comparison operator significantly improves assertion resolution run time for
34        // some cases. The current resolution algorithm's speed partially depends on the order of
35        // assertions. Assertions which have fewer possible matches should appear before
36        // assertions which have more possible matches. This seems to imply that this could
37        // be further improved by providing an indexer as an additional argument and ordering based
38        // on the number of matches of the same kind (object, function) for the names of the
39        // declarations.
40        //
41        // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this
42        // comparator.
43        //
44        // Note: since this compares pointers for position, minor changes in the source file that affect
45        // memory layout can alter compilation time in unpredictable ways. For example, the placement
46        // of a line directive can reorder type pointers with respect to each other so that assertions
47        // are seen in different orders, causing a potentially different number of unification calls
48        // when resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering
49        // line directives alone, so it would be nice to fix this comparison so that assertions compare
50        // more consistently. I've tried to modify this to compare on mangle name instead of type as
51        // the second comparator, but this causes some assertions to never be recorded. More
52        // investigation is needed.
53        struct AssertCompare {
54                bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const {
55                        int cmp = d1->get_name().compare( d2->get_name() );
56                        return cmp < 0 ||
57                                ( cmp == 0 && d1->get_type() < d2->get_type() );
58                }
59        };
60        struct AssertionSetValue {
61                bool isUsed;
62                // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID
63                // of an assertion on the current type, with each successive ID being the ID of an
64                // assertion pulled in by the previous ID. The last ID in the chain is the ID of the
65                // assertion that pulled in the current assertion.
66                std::list< UniqueId > idChain;
67        };
68        typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet;
69        typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
70
71        /// merges one set of open vars into another
72        static inline void mergeOpenVars( OpenVarSet& dst, const OpenVarSet& src ) {
73                for ( const auto& entry : src ) { dst[ entry.first ] = entry.second; }
74        }
75
76        void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
77        void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
78
79        struct EqvClass {
80                std::set< std::string > vars;
81                Type *type;
82                bool allowWidening;
83                TypeDecl::Data data;
84
85                void initialize( const EqvClass &src, EqvClass &dest );
86                void initialize( const EqvClass &src, EqvClass &dest, const Type *ty );
87                EqvClass();
88                EqvClass( const EqvClass &other );
89                EqvClass( const EqvClass &other, const Type *ty );
90                EqvClass( EqvClass &&other );
91                EqvClass &operator=( const EqvClass &other );
92                EqvClass &operator=( EqvClass &&other );
93                ~EqvClass();
94                void print( std::ostream &os, Indenter indent = {} ) const;
95
96                /// Takes ownership of `ty`, freeing old `type`
97                void set_type(Type* ty);
98        };
99
100        class TypeEnvironment {
101                using ClassList = std::list< EqvClass >;
102          public:
103                const EqvClass* lookup( const std::string &var ) const;
104          private:
105                void add( EqvClass &&eqvClass  );
106          public:
107                void add( const Type::ForallList &tyDecls );
108                void add( const TypeSubstitution & sub );
109                template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
110                template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
111                void makeSubstitution( TypeSubstitution &result ) const;
112                bool isEmpty() const { return env.empty(); }
113                void print( std::ostream &os, Indenter indent = {} ) const;
114               
115                /// Simply concatenate the second environment onto this one; no safety checks performed
116                void simpleCombine( const TypeEnvironment &second );
117
118          private:
119                /// Unifies the type bound of to with the type bound of from, returning false if fails
120                bool mergeBound( EqvClass& to, const EqvClass& from, OpenVarSet& openVars, const SymTab::Indexer& indexer );
121
122                /// Merges two type classes from local environment, returning false if fails
123                bool mergeClasses( ClassList::iterator to, ClassList::iterator from, OpenVarSet& openVars, const SymTab::Indexer& indexer );
124
125          public:
126                /// Merges the second environment with this one, checking compatibility.
127                /// Returns false if fails, but does NOT roll back partial changes.
128                bool combine( const TypeEnvironment& second, OpenVarSet& openVars, const SymTab::Indexer& indexer );
129               
130                void extractOpenVars( OpenVarSet &openVars ) const;
131                TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
132
133                /// Iteratively adds the environment of a new actual (with allowWidening = false),
134                /// and extracts open variables.
135                void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
136
137                /// Binds the type class represented by `typeInst` to the type `bindTo`; will add
138                /// the class if needed. Returns false on failure.
139                bool bindVar( TypeInstType *typeInst, Type *bindTo, const TypeDecl::Data & data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
140               
141                /// Binds the type classes represented by `var1` and `var2` together; will add
142                /// one or both classes if needed. Returns false on failure.
143                bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, const TypeDecl::Data & data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
144
145                /// Disallows widening for all bindings in the environment
146                void forbidWidening();
147
148                using iterator = ClassList::const_iterator;
149                iterator begin() const { return env.begin(); }
150                iterator end() const { return env.end(); }
151
152          private:
153                ClassList env;
154               
155                ClassList::iterator internal_lookup( const std::string &var );
156        };
157
158        template< typename SynTreeClass >
159        int TypeEnvironment::apply( SynTreeClass *&type ) const {
160                TypeSubstitution sub;
161                makeSubstitution( sub );
162                return sub.apply( type );
163        }
164
165        template< typename SynTreeClass >
166        int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
167                TypeSubstitution sub;
168                makeSubstitution( sub );
169                return sub.applyFree( type );
170        }
171
172        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
173} // namespace ResolvExpr
174
175// Local Variables: //
176// tab-width: 4 //
177// mode: c++ //
178// compile-command: "make install" //
179// End: //
Note: See TracBrowser for help on using the repository browser.