source: src/ResolvExpr/TypeEnvironment.h @ 19c43b7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 19c43b7 was 111a8af8, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Simplify AssertionSet? comparator

  • Property mode set to 100644
File size: 4.6 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
29namespace ResolvExpr {
[111a8af8]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.
[03da511]39        struct AssertCompare {
[111a8af8]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                }
[03da511]45        };
[6c3a988f]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;
[2c57025]54        typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
[51b7345]55
[a32b204]56        void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
57        void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 );
[51b7345]58
[a32b204]59        struct EqvClass {
60                std::set< std::string > vars;
61                Type *type;
62                bool allowWidening;
[2c57025]63                TypeDecl::Data data;
[03da511]64
[a32b204]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, int indent = 0 ) const;
71        };
[51b7345]72
[a32b204]73        class TypeEnvironment {
74          public:
75                bool lookup( const std::string &var, EqvClass &eqvClass ) const;
76                void add( const EqvClass &eqvClass );
[8c49c0e]77                void add( const Type::ForallList &tyDecls );
[a32b204]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, int indent = 0 ) 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 ); }
[03da511]87
[a32b204]88                typedef std::list< EqvClass >::iterator iterator;
89                iterator begin() { return env.begin(); }
90                iterator end() { return env.end(); }
91                typedef std::list< EqvClass >::const_iterator const_iterator;
92                const_iterator begin() const { return env.begin(); }
93                const_iterator end() const { return env.end(); }
94          private:
95                std::list< EqvClass > env;
96                std::list< EqvClass >::iterator internal_lookup( const std::string &var );
97        };
[51b7345]98
[a32b204]99        template< typename SynTreeClass >
100        int TypeEnvironment::apply( SynTreeClass *&type ) const {
101                TypeSubstitution sub;
102                makeSubstitution( sub );
103                return sub.apply( type );
104        }
[51b7345]105
[a32b204]106        template< typename SynTreeClass >
107        int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
108                TypeSubstitution sub;
109                makeSubstitution( sub );
110                return sub.applyFree( type );
111        }
[51b7345]112} // namespace ResolvExpr
113
[a32b204]114// Local Variables: //
115// tab-width: 4 //
116// mode: c++ //
117// compile-command: "make install" //
118// End: //
Note: See TracBrowser for help on using the repository browser.