source: src/ResolvExpr/TypeEnvironment.h @ 1d7b0a8

new-env
Last change on this file since 1d7b0a8 was 8e18b8e, checked in by Aaron Moss <a3moss@…>, 6 years ago

stop eagerly copying EqvClass? on lookup

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[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
[f229fc2]29template< typename Pass >
30class PassVisitor;
31class GcTracer;
32
[51b7345]33namespace ResolvExpr {
[111a8af8]34        // adding this comparison operator significantly improves assertion resolution run time for
35        // some cases. The current resolution algorithm's speed partially depends on the order of
36        // assertions. Assertions which have fewer possible matches should appear before
37        // assertions which have more possible matches. This seems to imply that this could
38        // be further improved by providing an indexer as an additional argument and ordering based
39        // on the number of matches of the same kind (object, function) for the names of the
40        // declarations.
41        //
42        // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this comparator.
[bd6e226]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 when
48        // resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering line directives
49        // alone, so it would be nice to fix this comparison so that assertions compare more consistently.
50        // I've tried to modify this to compare on mangle name instead of type as the second comparator, but
51        // this causes some assertions to never be recorded. More investigation is needed.
[03da511]52        struct AssertCompare {
[111a8af8]53                bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const {
54                        int cmp = d1->get_name().compare( d2->get_name() );
55                        return cmp < 0 ||
56                                ( cmp == 0 && d1->get_type() < d2->get_type() );
57                }
[03da511]58        };
[6c3a988f]59        struct AssertionSetValue {
60                bool isUsed;
61                // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID of an assertion on the current type,
62                // with each successive ID being the ID of an assertion pulled in by the previous ID. The last ID in the chain is
63                // the ID of the assertion that pulled in the current assertion.
64                std::list< UniqueId > idChain;
65        };
66        typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet;
[2c57025]67        typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
[51b7345]68
[a32b204]69        void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
70        void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
[51b7345]71
[a32b204]72        struct EqvClass {
73                std::set< std::string > vars;
74                Type *type;
75                bool allowWidening;
[2c57025]76                TypeDecl::Data data;
[03da511]77
[a32b204]78                void initialize( const EqvClass &src, EqvClass &dest );
79                EqvClass();
80                EqvClass( const EqvClass &other );
81                EqvClass &operator=( const EqvClass &other );
[50377a4]82                void print( std::ostream &os, Indenter indent = {} ) const;
[a32b204]83        };
[51b7345]84
[a32b204]85        class TypeEnvironment {
86          public:
[8e18b8e]87                const EqvClass* lookup( const std::string &var ) const;
[a32b204]88                void add( const EqvClass &eqvClass );
[8e18b8e]89                void add( EqvClass &&eqvClass  );
[8c49c0e]90                void add( const Type::ForallList &tyDecls );
[09c72d5]91                void add( const TypeSubstitution & sub );
[a32b204]92                template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
93                template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
94                void makeSubstitution( TypeSubstitution &result ) const;
95                bool isEmpty() const { return env.empty(); }
[50377a4]96                void print( std::ostream &os, Indenter indent = {} ) const;
[a32b204]97                void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
98                void simpleCombine( const TypeEnvironment &second );
99                void extractOpenVars( OpenVarSet &openVars ) const;
100                TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
[03da511]101
[98a249fb]102                /// Iteratively adds the environment of a new actual (with allowWidening = false),
[a585396]103                /// and extracts open variables.
104                void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
105
[a32b204]106                typedef std::list< EqvClass >::iterator iterator;
107                iterator begin() { return env.begin(); }
108                iterator end() { return env.end(); }
109                typedef std::list< EqvClass >::const_iterator const_iterator;
110                const_iterator begin() const { return env.begin(); }
111                const_iterator end() const { return env.end(); }
112          private:
113                std::list< EqvClass > env;
114                std::list< EqvClass >::iterator internal_lookup( const std::string &var );
115        };
[51b7345]116
[a32b204]117        template< typename SynTreeClass >
118        int TypeEnvironment::apply( SynTreeClass *&type ) const {
119                TypeSubstitution sub;
120                makeSubstitution( sub );
121                return sub.apply( type );
122        }
[51b7345]123
[a32b204]124        template< typename SynTreeClass >
125        int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
126                TypeSubstitution sub;
127                makeSubstitution( sub );
128                return sub.applyFree( type );
129        }
[98a249fb]130
131        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
[f229fc2]132
133        PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env );
[51b7345]134} // namespace ResolvExpr
135
[a32b204]136// Local Variables: //
137// tab-width: 4 //
138// mode: c++ //
139// compile-command: "make install" //
140// End: //
Note: See TracBrowser for help on using the repository browser.