Ignore:
Timestamp:
Oct 29, 2019, 4:01:24 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
773db65, 9421f3d8
Parents:
7951100 (diff), 8364209 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/TypeEnvironment.h

    r7951100 rb067d9b  
    99// Author           : Richard C. Bilson
    1010// 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
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Jul 19 17:00:10 2019
     13// Update Count     : 10
    1414//
    1515
     
    1818#include <iostream>                    // for ostream
    1919#include <list>                        // for list, list<>::iterator, list<>...
    20 #include <map>                         // for map, map<>::value_compare
    21 #include <set>                         // for set
     20#include <map>                                             // for map, map<>::value_compare
     21#include <unordered_map>
     22#include <set>                                             // for set
    2223#include <string>                      // for string
     24#include <utility>                     // for move, swap
     25
     26#include "WidenMode.h"                 // for WidenMode
    2327
    2428#include "SynTree/Declaration.h"       // for TypeDecl::Data, DeclarationWit...
     
    3640        // declarations.
    3741        //
    38         // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this comparator.
     42        // I've seen a TU go from 54 minutes to 1 minute 34 seconds with the addition of this
     43        // comparator.
    3944        //
    4045        // Note: since this compares pointers for position, minor changes in the source file that affect
    4146        // memory layout can alter compilation time in unpredictable ways. For example, the placement
    4247        // of a line directive can reorder type pointers with respect to each other so that assertions
    43         // are seen in different orders, causing a potentially different number of unification calls when
    44         // resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering line directives
    45         // alone, so it would be nice to fix this comparison so that assertions compare more consistently.
    46         // I've tried to modify this to compare on mangle name instead of type as the second comparator, but
    47         // this causes some assertions to never be recorded. More investigation is needed.
     48        // are seen in different orders, causing a potentially different number of unification calls
     49        // when resolving assertions. I've seen a TU go from 36 seconds to 27 seconds by reordering
     50        // line directives alone, so it would be nice to fix this comparison so that assertions compare
     51        // more consistently. I've tried to modify this to compare on mangle name instead of type as
     52        // the second comparator, but this causes some assertions to never be recorded. More
     53        // investigation is needed.
    4854        struct AssertCompare {
    49                 bool operator()( DeclarationWithType * d1, DeclarationWithType * d2 ) const {
     55                bool operator()( const DeclarationWithType * d1, const DeclarationWithType * d2 ) const {
    5056                        int cmp = d1->get_name().compare( d2->get_name() );
    5157                        return cmp < 0 ||
     
    5460        };
    5561        struct AssertionSetValue {
    56                 bool isUsed;
    57                 // chain of Unique IDs of the assertion declarations. The first ID in the chain is the ID of an assertion on the current type,
    58                 // with each successive ID being the ID of an assertion pulled in by the previous ID. The last ID in the chain is
    59                 // the ID of the assertion that pulled in the current assertion.
    60                 std::list< UniqueId > idChain;
     62                bool isUsed;        ///< True if assertion needs to be resolved
     63                UniqueId resnSlot;  ///< ID of slot assertion belongs to
     64
     65                AssertionSetValue() : isUsed(false), resnSlot(0) {}
    6166        };
    62         typedef std::map< DeclarationWithType*, AssertionSetValue, AssertCompare > AssertionSet;
    63         typedef std::map< std::string, TypeDecl::Data > OpenVarSet;
     67        typedef std::map< const DeclarationWithType *, AssertionSetValue, AssertCompare > AssertionSet;
     68        typedef std::unordered_map< std::string, TypeDecl::Data > OpenVarSet;
     69
     70        /// merges one set of open vars into another
     71        static inline void mergeOpenVars( OpenVarSet& dst, const OpenVarSet& src ) {
     72                for ( const auto& entry : src ) { dst[ entry.first ] = entry.second; }
     73        }
    6474
    6575        void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 );
     
    6878        struct EqvClass {
    6979                std::set< std::string > vars;
    70                 Type *type;
     80                Type * type;
    7181                bool allowWidening;
    7282                TypeDecl::Data data;
     
    7787                EqvClass( const EqvClass &other );
    7888                EqvClass( const EqvClass &other, const Type *ty );
     89                EqvClass( EqvClass &&other );
    7990                EqvClass &operator=( const EqvClass &other );
     91                EqvClass &operator=( EqvClass &&other );
    8092                ~EqvClass();
    8193                void print( std::ostream &os, Indenter indent = {} ) const;
     94
     95                /// Takes ownership of `ty`, freeing old `type`
     96                void set_type(Type* ty);
    8297        };
    8398
    8499        class TypeEnvironment {
     100                using ClassList = std::list< EqvClass >;
    85101          public:
    86102                const EqvClass* lookup( const std::string &var ) const;
    87                 void add( const EqvClass &eqvClass );
     103          private:
    88104                void add( EqvClass &&eqvClass  );
     105          public:
    89106                void add( const Type::ForallList &tyDecls );
    90107                void add( const TypeSubstitution & sub );
     
    94111                bool isEmpty() const { return env.empty(); }
    95112                void print( std::ostream &os, Indenter indent = {} ) const;
    96                 void combine( const TypeEnvironment &second, Type *(*combineFunc)( Type*, Type* ) );
     113
     114                /// Simply concatenate the second environment onto this one; no safety checks performed
    97115                void simpleCombine( const TypeEnvironment &second );
     116
     117          private:
     118                /// Unifies the type bound of to with the type bound of from, returning false if fails
     119                bool mergeBound( EqvClass& to, const EqvClass& from, OpenVarSet& openVars, const SymTab::Indexer& indexer );
     120
     121                /// Merges two type classes from local environment, returning false if fails
     122                bool mergeClasses( ClassList::iterator to, ClassList::iterator from, OpenVarSet& openVars, const SymTab::Indexer& indexer );
     123
     124          public:
     125                /// Merges the second environment with this one, checking compatibility.
     126                /// Returns false if fails, but does NOT roll back partial changes.
     127                bool combine( const TypeEnvironment& second, OpenVarSet& openVars, const SymTab::Indexer& indexer );
     128
    98129                void extractOpenVars( OpenVarSet &openVars ) const;
    99130                TypeEnvironment *clone() const { return new TypeEnvironment( *this ); }
     
    103134                void addActual( const TypeEnvironment& actualEnv, OpenVarSet& openVars );
    104135
    105                 typedef std::list< EqvClass >::iterator iterator;
    106                 iterator begin() { return env.begin(); }
    107                 iterator end() { return env.end(); }
    108                 typedef std::list< EqvClass >::const_iterator const_iterator;
    109                 const_iterator begin() const { return env.begin(); }
    110                 const_iterator end() const { return env.end(); }
     136                /// Binds the type class represented by `typeInst` to the type `bindTo`; will add
     137                /// the class if needed. Returns false on failure.
     138                bool bindVar( const TypeInstType * typeInst, Type * bindTo, const TypeDecl::Data & data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer );
     139
     140                /// Binds the type classes represented by `var1` and `var2` together; will add
     141                /// one or both classes if needed. Returns false on failure.
     142                bool bindVarToVar( const TypeInstType * var1, const TypeInstType * var2, TypeDecl::Data && data, AssertionSet &need, AssertionSet &have, const OpenVarSet &openVars, WidenMode widen, const SymTab::Indexer &indexer );
     143
     144                /// Disallows widening for all bindings in the environment
     145                void forbidWidening();
     146
     147                using iterator = ClassList::const_iterator;
     148                iterator begin() const { return env.begin(); }
     149                iterator end() const { return env.end(); }
     150
    111151          private:
    112                 std::list< EqvClass > env;
    113                 std::list< EqvClass >::iterator internal_lookup( const std::string &var );
     152                ClassList env;
     153
     154                ClassList::iterator internal_lookup( const std::string &var );
    114155        };
    115156
Note: See TracChangeset for help on using the changeset viewer.