source: src/ResolvExpr/TypeEnvironment.h @ f229fc2

new-envwith_gc
Last change on this file since f229fc2 was f229fc2, checked in by Aaron Moss <a3moss@…>, 4 years ago

Modify resolver to use young-generation collection per-top-level expression

  • Property mode set to 100644
File size: 5.0 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 : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:35:45 2017
13// Update Count     : 3
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
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
28
29template< typename Pass >
30class PassVisitor;
31class GcTracer;
32
33namespace ResolvExpr {
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.
43        struct AssertCompare {
44                bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const {
45                        int cmp = d1->get_name().compare( d2->get_name() );
46                        return cmp < 0 ||
47                                ( cmp == 0 && d1->get_type() < d2->get_type() );
48                }
49        };
50        struct AssertionSetValue {
51                bool isUsed;
52                // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID of an assertion on the current type,
53                // with each successive ID being the ID of an assertion pulled in by the previous ID. The last ID in the chain is
54                // the ID of the assertion that pulled in the current assertion.
55                std::list< UniqueId > idChain;
56        };
57        typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet;
58        typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
59
60        void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
61        void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
62
63        struct EqvClass {
64                std::set< std::string > vars;
65                Type *type;
66                bool allowWidening;
67                TypeDecl::Data data;
68
69                void initialize( const EqvClass &src, EqvClass &dest );
70                EqvClass();
71                EqvClass( const EqvClass &other );
72                EqvClass &operator=( const EqvClass &other );
73                void print( std::ostream &os, Indenter indent = {} ) const;
74        };
75
76        class TypeEnvironment {
77          public:
78                bool lookup( const std::string &var, EqvClass &eqvClass ) const;
79                void add( const EqvClass &eqvClass );
80                void add( const Type::ForallList &tyDecls );
81                void add( const TypeSubstitution & sub );
82                template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
83                template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
84                void makeSubstitution( TypeSubstitution &result ) const;
85                bool isEmpty() const { return env.empty(); }
86                void print( std::ostream &os, Indenter indent = {} ) const;
87                void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
88                void simpleCombine( const TypeEnvironment &second );
89                void extractOpenVars( OpenVarSet &openVars ) const;
90                TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
91
92                /// Iteratively adds the environment of a new actual (with allowWidening = false),
93                /// and extracts open variables.
94                void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
95
96                typedef std::list< EqvClass >::iterator iterator;
97                iterator begin() { return env.begin(); }
98                iterator end() { return env.end(); }
99                typedef std::list< EqvClass >::const_iterator const_iterator;
100                const_iterator begin() const { return env.begin(); }
101                const_iterator end() const { return env.end(); }
102          private:
103                std::list< EqvClass > env;
104                std::list< EqvClass >::iterator internal_lookup( const std::string &var );
105        };
106
107        template< typename SynTreeClass >
108        int TypeEnvironment::apply( SynTreeClass *&type ) const {
109                TypeSubstitution sub;
110                makeSubstitution( sub );
111                return sub.apply( type );
112        }
113
114        template< typename SynTreeClass >
115        int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
116                TypeSubstitution sub;
117                makeSubstitution( sub );
118                return sub.applyFree( type );
119        }
120
121        std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
122
123        PassVisitor<GcTracer> & operator<<( PassVisitor<GcTracer> & gc, const TypeEnvironment & env );
124} // namespace ResolvExpr
125
126// Local Variables: //
127// tab-width: 4 //
128// mode: c++ //
129// compile-command: "make install" //
130// End: //
Note: See TracBrowser for help on using the repository browser.