| 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 | template< typename SynTreeClass > int apply( SynTreeClass *&type ) const;
|
|---|
| 79 | template< typename SynTreeClass > int applyFree( SynTreeClass *&type ) const;
|
|---|
| 80 | void makeSubstitution( TypeSubstitution &result ) const;
|
|---|
| 81 | bool isEmpty() const { return env.empty(); }
|
|---|
| 82 | void print( std::ostream &os, Indenter indent = {} ) const;
|
|---|
| 83 | void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
|
|---|
| 84 | void simpleCombine( const TypeEnvironment &second );
|
|---|
| 85 | void extractOpenVars( OpenVarSet &openVars ) const;
|
|---|
| 86 | TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
|
|---|
| 87 |
|
|---|
| 88 | /// Iteratively adds the environment of a new actual (with allowWidening = false),
|
|---|
| 89 | /// and extracts open variables.
|
|---|
| 90 | void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
|
|---|
| 91 |
|
|---|
| 92 | typedef std::list< EqvClass >::iterator iterator;
|
|---|
| 93 | iterator begin() { return env.begin(); }
|
|---|
| 94 | iterator end() { return env.end(); }
|
|---|
| 95 | typedef std::list< EqvClass >::const_iterator const_iterator;
|
|---|
| 96 | const_iterator begin() const { return env.begin(); }
|
|---|
| 97 | const_iterator end() const { return env.end(); }
|
|---|
| 98 | private:
|
|---|
| 99 | std::list< EqvClass > env;
|
|---|
| 100 | std::list< EqvClass >::iterator internal_lookup( const std::string &var );
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 | template< typename SynTreeClass >
|
|---|
| 104 | int TypeEnvironment::apply( SynTreeClass *&type ) const {
|
|---|
| 105 | TypeSubstitution sub;
|
|---|
| 106 | makeSubstitution( sub );
|
|---|
| 107 | return sub.apply( type );
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | template< typename SynTreeClass >
|
|---|
| 111 | int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
|
|---|
| 112 | TypeSubstitution sub;
|
|---|
| 113 | makeSubstitution( sub );
|
|---|
| 114 | return sub.applyFree( type );
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | std::ostream & operator<<( std::ostream & out, const TypeEnvironment & env );
|
|---|
| 118 | } // namespace ResolvExpr
|
|---|
| 119 |
|
|---|
| 120 | // Local Variables: //
|
|---|
| 121 | // tab-width: 4 //
|
|---|
| 122 | // mode: c++ //
|
|---|
| 123 | // compile-command: "make install" //
|
|---|
| 124 | // End: //
|
|---|