source: src/ResolvExpr/TypeEnvironment.h @ 3f7e12cb

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 3f7e12cb was 3f7e12cb, checked in by Aaron Moss <a3moss@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 4.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
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();
[50377a4]70                void print( std::ostream &os, Indenter indent = {} ) const;
[a32b204]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(); }
[50377a4]82                void print( std::ostream &os, Indenter indent = {} ) const;
[a32b204]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
[a585396]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
[a32b204]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        };
[51b7345]102
[a32b204]103        template< typename SynTreeClass >
104        int TypeEnvironment::apply( SynTreeClass *&type ) const {
105                TypeSubstitution sub;
106                makeSubstitution( sub );
107                return sub.apply( type );
108        }
[51b7345]109
[a32b204]110        template< typename SynTreeClass >
111        int TypeEnvironment::applyFree( SynTreeClass *&type ) const {
112                TypeSubstitution sub;
113                makeSubstitution( sub );
114                return sub.applyFree( type );
115        }
[51b7345]116} // namespace ResolvExpr
117
[a32b204]118// Local Variables: //
119// tab-width: 4 //
120// mode: c++ //
121// compile-command: "make install" //
122// End: //
Note: See TracBrowser for help on using the repository browser.