Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ResolveAssertions.cc

    r493a992 r4d2d45f9  
    3535#include "SynTree/Expression.h"     // for InferredParams
    3636#include "TypeEnvironment.h"        // for TypeEnvironment, etc.
    37 #include "typeops.h"                // for adjustExprType, specCost
     37#include "typeops.h"                // for adjustExprType
    3838#include "Unify.h"                  // for unify
    3939
     
    5858        using CandidateList = std::vector<AssnCandidate>;
    5959
     60        /// Unique identifier for a yet-to-be-resolved assertion
     61        struct AssnId {
     62                DeclarationWithType* decl;  ///< Declaration of assertion
     63                AssertionSetValue info;     ///< Information about assertion
     64
     65                AssnId(DeclarationWithType* decl, const AssertionSetValue& info) : decl(decl), info(info) {}
     66        };
     67
     68        /// Cached assertion items
     69        struct AssnCacheItem {
     70                CandidateList matches;         ///< Possible matches for this assertion
     71                std::vector<AssnId> deferIds;  ///< Deferred assertions which resolve to this item
     72
     73                AssnCacheItem( CandidateList&& m ) : matches(std::move(m)), deferIds() {}
     74        };
     75
     76        /// Cache of resolved assertions
     77        using AssnCache = std::unordered_map<std::string, AssnCacheItem>;
     78
    6079        /// Reference to single deferred item
    6180        struct DeferRef {
    62                 const DeclarationWithType* decl;
    63                 const AssertionSetValue& info;
     81                const AssnCacheItem& item;
    6482                const AssnCandidate& match;
    6583        };
     
    6886        /// Acts like indexed list of DeferRef
    6987        struct DeferItem {
    70                 const DeclarationWithType* decl;
    71                 const AssertionSetValue& info;
    72                 CandidateList matches;
    73 
    74                 DeferItem( DeclarationWithType* decl, const AssertionSetValue& info, CandidateList&& matches )
    75                 : decl(decl), info(info), matches(std::move(matches)) {}
    76 
    77                 bool empty() const { return matches.empty(); }
    78 
    79                 CandidateList::size_type size() const { return matches.size(); }
    80 
    81                 DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; }
     88                const AssnCache* cache;     ///< Cache storing assertion item
     89                std::string key;            ///< Key into cache
     90               
     91                DeferItem( const AssnCache& cache, const std::string& key ) : cache(&cache), key(key) {}
     92
     93                bool empty() const { return cache->at(key).matches.empty(); }
     94
     95                CandidateList::size_type size() const { return cache->at(key).matches.size(); }
     96
     97                DeferRef operator[] ( unsigned i ) const {
     98                        const AssnCacheItem& item = cache->at(key);
     99                        return { item, item.matches[i] };
     100                }
     101
     102                const DeclarationWithType* get_decl() const { return cache->at(key).deferIds[0].decl; }
     103
     104                // sortable by key
     105                // TODO look into optimizing combination process with other sort orders (e.g. by number
     106                // of matches in candidate)
     107                bool operator< ( const DeferItem& o ) const { return key < o.key; }
     108                bool operator== ( const DeferItem& o ) const { return key == o.key; }
    82109        };
    83110
     
    154181                                for ( const auto& assn : x.assns ) {
    155182                                        k += computeConversionCost(
    156                                                 assn.match.adjType, assn.decl->get_type(), indexer, x.env );
    157                                        
    158                                         // mark vars+specialization cost on function-type assertions
    159                                         PointerType* ptr = dynamic_cast< PointerType* >( assn.decl->get_type() );
    160                                         if ( ! ptr ) continue;
    161                                         FunctionType* func = dynamic_cast< FunctionType* >( ptr->base );
    162                                         if ( ! func ) continue;
    163                                        
    164                                         for ( DeclarationWithType* formal : func->parameters ) {
    165                                                 k.decSpec( specCost( formal->get_type() ) );
    166                                         }
    167                                         k.incVar( func->forall.size() );
    168                                         for ( TypeDecl* td : func->forall ) {
    169                                                 k.decSpec( td->assertions.size() );
    170                                         }
     183                                                assn.match.adjType, assn.item.deferIds[0].decl->get_type(), indexer,
     184                                                x.env );
    171185                                }
    172186                                it = cache.emplace_hint( it, &x, k );
     
    239253
    240254        /// Resolve a single assertion, in context
    241         bool resolveAssertion( AssertionItem& assn, ResnState& resn ) {
     255        bool resolveAssertion( AssertionItem& assn, ResnState& resn, AssnCache& cache ) {
    242256                // skip unused assertions
    243257                if ( ! assn.info.isUsed ) return true;
    244258
    245                 // lookup candidates for this assertion
    246                 std::list< SymTab::Indexer::IdData > candidates;
    247                 resn.indexer.lookupId( assn.decl->name, candidates );
    248 
    249                 // find the candidates that unify with the desired type
    250                 CandidateList matches;
    251                 for ( const auto& cdata : candidates ) {
    252                         DeclarationWithType* candidate = cdata.id;
    253 
    254                         // build independent unification context for candidate
    255                         AssertionSet have, newNeed;
    256                         TypeEnvironment newEnv{ resn.alt.env };
    257                         OpenVarSet newOpenVars{ resn.alt.openVars };
    258                         Type* adjType = candidate->get_type()->clone();
    259                         adjustExprType( adjType, newEnv, resn.indexer );
    260                         renameTyVars( adjType );
    261 
    262                         // keep unifying candidates
    263                         if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars,
    264                                         resn.indexer ) ) {
    265                                 // set up binding slot for recursive assertions
    266                                 UniqueId crntResnSlot = 0;
    267                                 if ( ! newNeed.empty() ) {
    268                                         crntResnSlot = ++globalResnSlot;
    269                                         for ( auto& a : newNeed ) {
    270                                                 a.second.resnSlot = crntResnSlot;
     259                // check cache for this assertion
     260                std::string assnKey = SymTab::Mangler::mangleAssnKey( assn.decl, resn.alt.env );
     261                auto it = cache.find( assnKey );
     262
     263                // attempt to resolve assertion if this is the first time seen
     264                if ( it == cache.end() ) {
     265                        // lookup candidates for this assertion
     266                        std::list< SymTab::Indexer::IdData > candidates;
     267                        resn.indexer.lookupId( assn.decl->name, candidates );
     268
     269                        // find the candidates that unify with the desired type
     270                        CandidateList matches;
     271                        for ( const auto& cdata : candidates ) {
     272                                DeclarationWithType* candidate = cdata.id;
     273
     274                                // build independent unification context for candidate
     275                                AssertionSet have, newNeed;
     276                                TypeEnvironment newEnv{ resn.alt.env };
     277                                OpenVarSet newOpenVars{ resn.alt.openVars };
     278                                Type* adjType = candidate->get_type()->clone();
     279                                adjustExprType( adjType, newEnv, resn.indexer );
     280                                renameTyVars( adjType );
     281
     282                                // keep unifying candidates
     283                                if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars,
     284                                                resn.indexer ) ) {
     285                                        // set up binding slot for recursive assertions
     286                                        UniqueId crntResnSlot = 0;
     287                                        if ( ! newNeed.empty() ) {
     288                                                crntResnSlot = ++globalResnSlot;
     289                                                for ( auto& a : newNeed ) {
     290                                                        a.second.resnSlot = crntResnSlot;
     291                                                }
    271292                                        }
    272                                 }
    273 
    274                                 matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have),
    275                                         std::move(newNeed), std::move(newOpenVars), crntResnSlot );
    276                         } else {
    277                                 delete adjType;
     293
     294                                        matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have),
     295                                                std::move(newNeed), std::move(newOpenVars), crntResnSlot );
     296                                } else {
     297                                        delete adjType;
     298                                }
    278299                        }
    279                 }
     300
     301                        it = cache.emplace_hint( it, assnKey, AssnCacheItem{ std::move(matches) } );
     302                }
     303
     304                CandidateList& matches = it->second.matches;
    280305
    281306                // break if no suitable assertion
     
    284309                // defer if too many suitable assertions
    285310                if ( matches.size() > 1 ) {
    286                         resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) );
     311                        it->second.deferIds.emplace_back( assn.decl, assn.info );
     312                        resn.deferred.emplace_back( cache, assnKey );
    287313                        return true;
    288314                }
     
    292318                addToIndexer( match.have, resn.indexer );
    293319                resn.newNeed.insert( match.need.begin(), match.need.end() );
    294                 resn.alt.env = std::move(match.env);
    295                 resn.alt.openVars = std::move(match.openVars);
     320                resn.alt.env = match.env;
     321                resn.alt.openVars = match.openVars;
    296322
    297323                bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred );
     
    354380                ResnList resns{ ResnState{ alt, root_indexer } };
    355381                ResnList new_resns{};
     382                AssnCache assnCache;
    356383
    357384                // resolve assertions in breadth-first-order up to a limited number of levels deep
     
    362389                                for ( auto& assn : resn.need ) {
    363390                                        // fail early if any assertion is not resolvable
    364                                         if ( ! resolveAssertion( assn, resn ) ) {
     391                                        if ( ! resolveAssertion( assn, resn, assnCache ) ) {
    365392                                                Indenter tabs{ Indenter::tabsize, 3 };
    366393                                                std::ostringstream ss;
     
    383410                                        }
    384411                                } else {
     412                                        // only resolve each deferred assertion once
     413                                        std::sort( resn.deferred.begin(), resn.deferred.end() );
     414                                        auto last = std::unique( resn.deferred.begin(), resn.deferred.end() );
     415                                        resn.deferred.erase( last, resn.deferred.end() );
    385416                                        // resolve deferred assertions by mutual compatibility
    386417                                        std::vector<CandidateEnvMerger::OutType> compatible = filterCombos(
     
    396427                                                ++tabs;
    397428                                                for ( const auto& d : resn.deferred ) {
    398                                                         d.decl->print( ss, tabs );
     429                                                        d.get_decl()->print( ss, tabs );
    399430                                                }
    400431
     
    427458                                                        new_resn.newNeed.insert( match.need.begin(), match.need.end() );
    428459
    429                                                         bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred );
     460                                                        // for each deferred assertion with the same form
     461                                                        for ( AssnId id : r.item.deferIds ) {
     462                                                                bindAssertion(
     463                                                                        id.decl, id.info, new_resn.alt, match, new_resn.inferred );
     464                                                        }
    430465                                                }
    431466
Note: See TracChangeset for help on using the changeset viewer.