Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/SatisfyAssertions.cpp

    r1958fec raca6a54c  
    99// Author           : Aaron B. Moss
    1010// Created On       : Mon Jun 10 17:45:00 2019
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Oct  1 13:56:00 2019
    13 // Update Count     : 2
     11// Last Modified By : Aaron B. Moss
     12// Last Modified On : Mon Jun 10 17:45:00 2019
     13// Update Count     : 1
    1414//
    1515
     
    5757                ast::UniqueId resnSlot;          ///< Slot for any recursive assertion IDs
    5858
    59                 AssnCandidate( 
    60                         const ast::SymbolTable::IdData c, const ast::Type * at, ast::TypeEnvironment && e, 
     59                AssnCandidate(
     60                        const ast::SymbolTable::IdData c, const ast::Type * at, ast::TypeEnvironment && e,
    6161                        ast::AssertionSet && h, ast::AssertionSet && n, ast::OpenVarSet && o, ast::UniqueId rs )
    62                 : cdata( c ), adjType( at ), env( std::move( e ) ), have( std::move( h ) ), 
     62                : cdata( c ), adjType( at ), env( std::move( e ) ), have( std::move( h ) ),
    6363                  need( std::move( n ) ), open( std::move( o ) ), resnSlot( rs ) {}
    6464        };
     
    6969        /// Reference to a single deferred item
    7070        struct DeferRef {
    71                 const ast::VariableExpr * expr;
     71                const ast::DeclWithType * decl;
    7272                const ast::AssertionSetValue & info;
    7373                const AssnCandidate & match;
    7474        };
    75        
    76         /// Wrapper for the deferred items from a single assertion satisfaction. 
     75
     76        /// Wrapper for the deferred items from a single assertion satisfaction.
    7777        /// Acts like an indexed list of DeferRef
    7878        struct DeferItem {
    79                 const ast::VariableExpr * expr;
     79                const ast::DeclWithType * decl;
    8080                const ast::AssertionSetValue & info;
    8181                AssnCandidateList matches;
    8282
    83                 DeferItem( 
    84                         const ast::VariableExpr * d, const ast::AssertionSetValue & i, AssnCandidateList && ms )
    85                 : expr( d ), info( i ), matches( std::move( ms ) ) {}
     83                DeferItem(
     84                        const ast::DeclWithType * d, const ast::AssertionSetValue & i, AssnCandidateList && ms )
     85                : decl( d ), info( i ), matches( std::move( ms ) ) {}
    8686
    8787                bool empty() const { return matches.empty(); }
     
    8989                AssnCandidateList::size_type size() const { return matches.size(); }
    9090
    91                 DeferRef operator[] ( unsigned i ) const { return { expr, info, matches[i] }; }
     91                DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; }
    9292        };
    9393
     
    117117                /// Initial satisfaction state for a candidate
    118118                SatState( CandidateRef & c, const ast::SymbolTable & syms )
    119                 : cand( c ), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero }, 
     119                : cand( c ), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero },
    120120                  symtab( syms ) { need.swap( c->need ); }
    121                
     121
    122122                /// Update satisfaction state for next step after previous state
    123123                SatState( SatState && o, IterateFlag )
    124                 : cand( std::move( o.cand ) ), need( o.newNeed.begin(), o.newNeed.end() ), newNeed(), 
    125                   deferred(), inferred( std::move( o.inferred ) ), costs( std::move( o.costs ) ), 
     124                : cand( std::move( o.cand ) ), need( o.newNeed.begin(), o.newNeed.end() ), newNeed(),
     125                  deferred(), inferred( std::move( o.inferred ) ), costs( std::move( o.costs ) ),
    126126                  symtab( o.symtab ) { costs.emplace_back( Cost::zero ); }
    127                
     127
    128128                /// Field-wise next step constructor
    129129                SatState(
    130                         CandidateRef && c, ast::AssertionSet && nn, InferCache && i, CostVec && cs, 
     130                        CandidateRef && c, ast::AssertionSet && nn, InferCache && i, CostVec && cs,
    131131                        ast::SymbolTable && syms )
    132                 : cand( std::move( c ) ), need( nn.begin(), nn.end() ), newNeed(), deferred(), 
     132                : cand( std::move( c ) ), need( nn.begin(), nn.end() ), newNeed(), deferred(),
    133133                  inferred( std::move( i ) ), costs( std::move( cs ) ), symtab( std::move( syms ) )
    134134                  { costs.emplace_back( Cost::zero ); }
     
    138138        void addToSymbolTable( const ast::AssertionSet & have, ast::SymbolTable & symtab ) {
    139139                for ( auto & i : have ) {
    140                         if ( i.second.isUsed ) { symtab.addId( i.first->var ); }
     140                        if ( i.second.isUsed ) { symtab.addId( i.first ); }
    141141                }
    142142        }
    143143
    144144        /// Binds a single assertion, updating satisfaction state
    145         void bindAssertion( 
    146                 const ast::VariableExpr * expr, const ast::AssertionSetValue & info, CandidateRef & cand,
     145        void bindAssertion(
     146                const ast::DeclWithType * decl, const ast::AssertionSetValue & info, CandidateRef & cand,
    147147                AssnCandidate & match, InferCache & inferred
    148148        ) {
    149149                const ast::DeclWithType * candidate = match.cdata.id;
    150                 assertf( candidate->uniqueId, 
     150                assertf( candidate->uniqueId,
    151151                        "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() );
    152                
     152
    153153                ast::Expr * varExpr = match.cdata.combine( cand->expr->location, cand->cvtCost );
    154154                varExpr->result = match.adjType;
     
    156156
    157157                // place newly-inferred assertion in proper location in cache
    158                 inferred[ info.resnSlot ][ expr->var->uniqueId ] = ast::ParamEntry{
    159                         candidate->uniqueId, candidate, match.adjType, expr->result, varExpr };
     158                inferred[ info.resnSlot ][ decl->uniqueId ] = ast::ParamEntry{
     159                        candidate->uniqueId, candidate, match.adjType, decl->get_type(), varExpr };
    160160        }
    161161
     
    167167                // find candidates that unify with the desired type
    168168                AssnCandidateList matches;
    169 
    170                 std::vector<ast::SymbolTable::IdData> candidates;
    171                 auto kind = ast::SymbolTable::getSpecialFunctionKind(assn.first->var->name);
    172                 if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) {
    173                         // prefilter special decls by argument type, if already known
    174                         ast::ptr<ast::Type> thisArgType = assn.first->result.strict_as<ast::PointerType>()->base
    175                                 .strict_as<ast::FunctionType>()->params[0]
    176                                 .strict_as<ast::ReferenceType>()->base;
    177                         sat.cand->env.apply(thisArgType);
    178 
    179                         std::string otypeKey = "";
    180                         if (thisArgType.as<ast::PointerType>()) otypeKey = Mangle::Encoding::pointer;
    181                         else if (!isUnboundType(thisArgType)) otypeKey = Mangle::mangle(thisArgType, Mangle::Type | Mangle::NoGenericParams);
    182 
    183                         candidates = sat.symtab.specialLookupId(kind, otypeKey);
    184                 }
    185                 else {
    186                         candidates = sat.symtab.lookupId(assn.first->var->name);
    187                 }
    188                 for ( const ast::SymbolTable::IdData & cdata : candidates ) {
     169                for ( const ast::SymbolTable::IdData & cdata : sat.symtab.lookupId( assn.first->name ) ) {
    189170                        const ast::DeclWithType * candidate = cdata.id;
    190 
    191                         // ignore deleted candidates.
    192                         // NOTE: this behavior is different from main resolver.
    193                         // further investigations might be needed to determine
    194                         // if we should implement the same rule here
    195                         // (i.e. error if unique best match is deleted)
    196                         if (candidate->isDeleted && candidate->linkage == ast::Linkage::AutoGen) continue;
    197171
    198172                        // build independent unification context for candidate
     
    200174                        ast::TypeEnvironment newEnv{ sat.cand->env };
    201175                        ast::OpenVarSet newOpen{ sat.cand->open };
    202                         ast::ptr< ast::Type > toType = assn.first->result;
    203                         ast::ptr< ast::Type > adjType = 
    204                                 renameTyVars( adjustExprType( candidate->get_type(), newEnv, sat.symtab ), GEN_USAGE, false );
     176                        ast::ptr< ast::Type > toType = assn.first->get_type();
     177                        ast::ptr< ast::Type > adjType =
     178                                renameTyVars( adjustExprType( candidate->get_type(), newEnv, sat.symtab ) );
    205179
    206180                        // only keep candidates which unify
     
    213187                                }
    214188
    215                                 matches.emplace_back( 
    216                                         cdata, adjType, std::move( newEnv ), std::move( have ), std::move( newNeed ),
     189                                matches.emplace_back(
     190                                        cdata, adjType, std::move( newEnv ), std::move( newNeed ), std::move( have ),
    217191                                        std::move( newOpen ), crntResnSlot );
    218192                        }
     
    255229                InferMatcher( InferCache & inferred ) : inferred( inferred ) {}
    256230
    257                 const ast::Expr * postvisit( const ast::Expr * expr ) {
     231                const ast::Expr * postmutate( const ast::Expr * expr ) {
    258232                        // Skip if no slots to find
    259                         if ( !expr->inferred.hasSlots() ) return expr;
    260                         // if ( expr->inferred.mode != ast::Expr::InferUnion::Slots ) return expr;
    261                         std::vector<UniqueId> missingSlots;
     233                        if ( expr->inferred.mode != ast::Expr::InferUnion::Slots ) return expr;
     234
    262235                        // find inferred parameters for resolution slots
    263                         ast::InferredParams * newInferred = new ast::InferredParams();
     236                        ast::InferredParams newInferred;
    264237                        for ( UniqueId slot : expr->inferred.resnSlots() ) {
    265238                                // fail if no matching assertions found
    266239                                auto it = inferred.find( slot );
    267240                                if ( it == inferred.end() ) {
    268                                         // std::cerr << "missing assertion " << slot << std::endl;
    269                                         missingSlots.push_back(slot);
    270                                         continue;
     241                                        assert(!"missing assertion");
    271242                                }
    272243
     
    274245                                for ( auto & entry : it->second ) {
    275246                                        // recurse on inferParams of resolved expressions
    276                                         entry.second.expr = postvisit( entry.second.expr );
    277                                         auto res = newInferred->emplace( entry );
     247                                        entry.second.expr = postmutate( entry.second.expr );
     248                                        auto res = newInferred.emplace( entry );
    278249                                        assert( res.second && "all assertions newly placed" );
    279250                                }
     
    281252
    282253                        ast::Expr * ret = mutate( expr );
    283                         ret->inferred.set_inferParams( newInferred );
    284                         if (!missingSlots.empty()) ret->inferred.resnSlots() = missingSlots;
     254                        ret->inferred.set_inferParams( std::move( newInferred ) );
    285255                        return ret;
    286256                }
    287257        };
    288258
    289         /// Replace ResnSlots with InferParams and add alternative to output list, if it meets pruning 
     259        /// Replace ResnSlots with InferParams and add alternative to output list, if it meets pruning
    290260        /// threshold.
    291         void finalizeAssertions( 
    292                 CandidateRef & cand, InferCache & inferred, PruneMap & thresholds, CostVec && costs, 
    293                 CandidateList & out 
     261        void finalizeAssertions(
     262                CandidateRef & cand, InferCache & inferred, PruneMap & thresholds, CostVec && costs,
     263                CandidateList & out
    294264        ) {
    295265                // prune if cheaper alternative for same key has already been generated
     
    308278        }
    309279
    310         /// Combo iterator that combines candidates into an output list, merging their environments. 
    311         /// Rejects an appended candidate if environments cannot be merged. See `Common/FilterCombos.h` 
     280        /// Combo iterator that combines candidates into an output list, merging their environments.
     281        /// Rejects an appended candidate if environments cannot be merged. See `Common/FilterCombos.h`
    312282        /// for description of "combo iterator".
    313283        class CandidateEnvMerger {
     
    337307                                        // compute conversion cost from satisfying decl to assertion
    338308                                        cost += computeConversionCost(
    339                                                 assn.match.adjType, assn.expr->result, false, symtab, env );
     309                                                assn.match.adjType, assn.decl->get_type(), symtab, env );
    340310
    341311                                        // mark vars+specialization on function-type assertions
     
    344314                                        if ( ! func ) continue;
    345315
    346                                         for ( const auto & param : func->params ) {
    347                                                 cost.decSpec( specCost( param ) );
     316                                        for ( const ast::DeclWithType * param : func->params ) {
     317                                                cost.decSpec( specCost( param->get_type() ) );
    348318                                        }
    349319
    350320                                        cost.incVar( func->forall.size() );
    351321
    352                                         cost.decSpec( func->assertions.size() );
     322                                        for ( const ast::TypeDecl * td : func->forall ) {
     323                                                cost.decSpec( td->assertions.size() );
     324                                        }
    353325                                }
    354326                        }
     
    385357
    386358        /// Limit to depth of recursion of assertion satisfaction
    387         static const int recursionLimit = 8;
     359        static const int recursionLimit = 7;
    388360        /// Maximum number of simultaneously-deferred assertions to attempt concurrent satisfaction of
    389361        static const int deferLimit = 10;
    390362} // anonymous namespace
    391363
    392 void satisfyAssertions( 
    393         CandidateRef & cand, const ast::SymbolTable & symtab, CandidateList & out, 
     364void satisfyAssertions(
     365        CandidateRef & cand, const ast::SymbolTable & symtab, CandidateList & out,
    394366        std::vector<std::string> & errors
    395367) {
     
    417389                        if ( it != thresholds.end() && it->second < sat.costs ) goto nextSat;
    418390
    419                         // should a limit be imposed? worst case here is O(n^2) but very unlikely to happen.
    420                         for (unsigned resetCount = 0; ; ++resetCount) {
    421                                 ast::AssertionList next;
    422                                 resetTyVarRenaming();
    423                                 // make initial pass at matching assertions
    424                                 for ( auto & assn : sat.need ) {
    425                                         // fail early if any assertion is not satisfiable
    426                                         if ( ! satisfyAssertion( assn, sat ) ) {
    427                                                 next.emplace_back(assn);
    428                                                 // goto nextSat;
    429                                         }
    430                                 }
    431                                 // success
    432                                 if (next.empty()) break;
    433                                 // fail if nothing resolves
    434                                 else if (next.size() == sat.need.size()) {
     391                        // make initial pass at matching assertions
     392                        for ( auto & assn : sat.need ) {
     393                                // fail early if any assertion is not satisfiable
     394                                if ( ! satisfyAssertion( assn, sat ) ) {
    435395                                        Indenter tabs{ 3 };
    436396                                        std::ostringstream ss;
     
    438398                                        print( ss, *sat.cand, ++tabs );
    439399                                        ss << (tabs-1) << "Could not satisfy assertion:\n";
    440                                         ast::print( ss, next[0].first, tabs );
     400                                        ast::print( ss, assn.first, tabs );
    441401
    442402                                        errors.emplace_back( ss.str() );
    443403                                        goto nextSat;
    444404                                }
    445                                 sat.need = std::move(next);
    446405                        }
    447406
     
    449408                                // either add successful match or push back next state
    450409                                if ( sat.newNeed.empty() ) {
    451                                         finalizeAssertions( 
     410                                        finalizeAssertions(
    452411                                                sat.cand, sat.inferred, thresholds, std::move( sat.costs ), out );
    453412                                } else {
     
    462421                                ss << (tabs-1) << "Too many non-unique satisfying assignments for assertions:\n";
    463422                                for ( const auto & d : sat.deferred ) {
    464                                         ast::print( ss, d.expr, tabs );
     423                                        ast::print( ss, d.decl, tabs );
    465424                                }
    466425
     
    471430                                std::vector< CandidateEnvMerger::OutType > compatible = filterCombos(
    472431                                        sat.deferred, CandidateEnvMerger{ sat.cand->env, sat.cand->open, sat.symtab } );
    473                                
     432
    474433                                // fail early if no mutually-compatible assertion satisfaction
    475434                                if ( compatible.empty() ) {
     
    480439                                        ss << (tabs-1) << "No mutually-compatible satisfaction for assertions:\n";
    481440                                        for ( const auto& d : sat.deferred ) {
    482                                                 ast::print( ss, d.expr, tabs );
     441                                                ast::print( ss, d.decl, tabs );
    483442                                        }
    484443
     
    494453                                        // set up next satisfaction state
    495454                                        CandidateRef nextCand = std::make_shared<Candidate>(
    496                                                 sat.cand->expr, std::move( compat.env ), std::move( compat.open ), 
     455                                                sat.cand->expr, std::move( compat.env ), std::move( compat.open ),
    497456                                                ast::AssertionSet{} /* need moved into satisfaction state */,
    498457                                                sat.cand->cost, sat.cand->cvtCost );
     
    500459                                        ast::AssertionSet nextNewNeed{ sat.newNeed };
    501460                                        InferCache nextInferred{ sat.inferred };
    502                                        
     461
    503462                                        CostVec nextCosts{ sat.costs };
    504463                                        nextCosts.back() += compat.cost;
    505                                                                
     464
    506465                                        ast::SymbolTable nextSymtab{ sat.symtab };
    507466
     
    512471                                                nextNewNeed.insert( match.need.begin(), match.need.end() );
    513472
    514                                                 bindAssertion( r.expr, r.info, nextCand, match, nextInferred );
     473                                                bindAssertion( r.decl, r.info, nextCand, match, nextInferred );
    515474                                        }
    516475
    517476                                        // either add successful match or push back next state
    518477                                        if ( nextNewNeed.empty() ) {
    519                                                 finalizeAssertions( 
     478                                                finalizeAssertions(
    520479                                                        nextCand, nextInferred, thresholds, std::move( nextCosts ), out );
    521480                                        } else {
    522                                                 nextSats.emplace_back( 
    523                                                         std::move( nextCand ), std::move( nextNewNeed ), 
    524                                                         std::move( nextInferred ), std::move( nextCosts ), 
     481                                                nextSats.emplace_back(
     482                                                        std::move( nextCand ), std::move( nextNewNeed ),
     483                                                        std::move( nextInferred ), std::move( nextCosts ),
    525484                                                        std::move( nextSymtab ) );
    526485                                        }
     
    534493                nextSats.clear();
    535494        }
    536        
     495
    537496        // exceeded recursion limit if reaches here
    538497        if ( out.empty() ) {
Note: See TracChangeset for help on using the changeset viewer.