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 |
|
---|
29 | namespace ResolvExpr {
|
---|
30 | // adding this comparison operator significantly improves assertion resolution run time for
|
---|
31 | // some cases. The current resolution algorithm's speed partially depends on the order of
|
---|
32 | // assertions. Assertions which have fewer possible matches should appear before
|
---|
33 | // assertions which have more possible matches. This seems to imply that this could
|
---|
34 | // be further improved by providing an indexer as an additional argument and ordering based
|
---|
35 | // on the number of matches of the same kind (object, function) for the names of the
|
---|
36 | // declarations.
|
---|
37 | //
|
---|
38 | // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this comparator.
|
---|
39 | struct AssertCompare {
|
---|
40 | bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const {
|
---|
41 | int cmp = d1->get_name().compare( d2->get_name() );
|
---|
42 | return cmp < 0 ||
|
---|
43 | ( cmp == 0 && d1->get_type() < d2->get_type() );
|
---|
44 | }
|
---|
45 | };
|
---|
46 | struct AssertionSetValue {
|
---|
47 | bool isUsed;
|
---|
48 | // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID of an assertion on the current type,
|
---|
49 | // with each successive ID being the ID of an assertion pulled in by the previous ID. The last ID in the chain is
|
---|
50 | // the ID of the assertion that pulled in the current assertion.
|
---|
51 | std::list< UniqueId > idChain;
|
---|
52 | };
|
---|
53 | typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet;
|
---|
54 | typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
|
---|
55 |
|
---|
56 | void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
|
---|
57 | void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
|
---|
58 |
|
---|
59 | struct EqvClass {
|
---|
60 | std::set< std::string > vars;
|
---|
61 | Type *type;
|
---|
62 | bool allowWidening;
|
---|
63 | TypeDecl::Data data;
|
---|
64 |
|
---|
65 | void initialize( const EqvClass &src, EqvClass &dest );
|
---|
66 | EqvClass();
|
---|
67 | EqvClass( const EqvClass &other );
|
---|
68 | EqvClass &operator=( const EqvClass &other );
|
---|
69 | ~EqvClass();
|
---|
70 | void print( std::ostream &os, Indenter indent = {} ) const;
|
---|
71 | };
|
---|
72 |
|
---|
73 | class TypeEnvironment {
|
---|
74 | public:
|
---|
75 | bool lookup( const std::string &var, EqvClass &eqvClass ) const;
|
---|
76 | void add( const EqvClass &eqvClass );
|
---|
77 | void add( const Type::ForallList &tyDecls );
|
---|
78 | void add( const TypeSubstitution & sub );
|
---|
79 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
|
---|
80 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
|
---|
81 | void makeSubstitution( TypeSubstitution &result ) const;
|
---|
82 | bool isEmpty() const { return env.empty(); }
|
---|
83 | void print( std::ostream &os, Indenter indent = {} ) const;
|
---|
84 | void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
|
---|
85 | void simpleCombine( const TypeEnvironment &second );
|
---|
86 | void extractOpenVars( OpenVarSet &openVars ) const;
|
---|
87 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
|
---|
88 |
|
---|
89 | /// Iteratively adds the environment of a new actual (with allowWidening = false),
|
---|
90 | /// and extracts open variables.
|
---|
91 | void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
|
---|
92 |
|
---|
93 | typedef std::list< EqvClass >::iterator iterator;
|
---|
94 | iterator begin() { return env.begin(); }
|
---|
95 | iterator end() { return env.end(); }
|
---|
96 | typedef std::list< EqvClass >::const_iterator const_iterator;
|
---|
97 | const_iterator begin() const { return env.begin(); }
|
---|
98 | const_iterator end() const { return env.end(); }
|
---|
99 | private:
|
---|
100 | std::list< EqvClass > env;
|
---|
101 | std::list< EqvClass >::iterator internal_lookup( const std::string &var );
|
---|
102 | };
|
---|
103 |
|
---|
104 | template< typename SynTreeClass >
|
---|
105 | int TypeEnvironment::apply( SynTreeClass *&type ) const {
|
---|
106 | TypeSubstitution sub;
|
---|
107 | makeSubstitution( sub );
|
---|
108 | return sub.apply( type );
|
---|
109 | }
|
---|
110 |
|
---|
111 | template< typename SynTreeClass >
|
---|
112 | int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
|
---|
113 | TypeSubstitution sub;
|
---|
114 | makeSubstitution( sub );
|
---|
115 | return sub.applyFree( type );
|
---|
116 | }
|
---|
117 |
|
---|
118 | std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
|
---|
119 | } // namespace ResolvExpr
|
---|
120 |
|
---|
121 | // Local Variables: //
|
---|
122 | // tab-width: 4 //
|
---|
123 | // mode: c++ //
|
---|
124 | // compile-command: "make install" //
|
---|
125 | // End: //
|
---|