Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CandidateFinder.cpp

    r396037d r99d4584  
    1616#include "CandidateFinder.hpp"
    1717
    18 #include <sstream>
    19 
    20 #include "Candidate.hpp"
    21 #include "CompilationState.h"
    22 #include "SatisfyAssertions.hpp"
    2318#include "AST/Expr.hpp"
    24 #include "AST/Node.hpp"
    25 #include "AST/Pass.hpp"
    26 
    27 #define PRINT( text ) if ( resolvep ) { text }
    2819
    2920namespace ResolvExpr {
    3021
    31 namespace {
    32 
    33         /// Actually visits expressions to find their candidate interpretations
    34         struct Finder {
    35                 CandidateFinder & candFinder;
    36                 const ast::SymbolTable & symtab;
    37                 CandidateList & candidates;
    38                 const ast::TypeEnvironment & tenv;
    39                 ast::ptr< ast::Type > & targetType;
    40 
    41                 Finder( CandidateFinder & f )
    42                 : candFinder( f ), symtab( f.symtab ), candidates( f.candidates ), tenv( f.env ),
    43                   targetType( f.targetType ) {}
    44                
    45                 #warning unimplemented
    46         };
    47 
    48         /// Prunes a list of candidates down to those that have the minimum conversion cost for a given
    49         /// return type. Skips ambiguous candidates.
    50         CandidateList pruneCandidates( CandidateList & candidates ) {
    51                 #warning unimplemented
    52                 (void)candidates;
    53                 assert(false);
    54                 return {};
    55         }
    56 
    57 } // anonymous namespace
    58 
    5922void CandidateFinder::find( const ast::Expr * expr, ResolvMode mode ) {
    60         // Find alternatives for expression
    61         ast::Pass<Finder> finder{ *this };
    62         expr->accept( finder );
    63 
    64         if ( mode.failFast && candidates.empty() ) {
    65                 SemanticError( expr, "No reasonable alternatives for expression " );
    66         }
    67 
    68         if ( mode.satisfyAssns || mode.prune ) {
    69                 // trim candidates to just those where the assertions are satisfiable
    70                 // - necessary pre-requisite to pruning
    71                 CandidateList satisfied;
    72                 std::vector< std::string > errors;
    73                 for ( auto & candidate : candidates ) {
    74                         satisfyAssertions( *candidate, symtab, satisfied, errors );
    75                 }
    76 
    77                 // fail early if none such
    78                 if ( mode.failFast && satisfied.empty() ) {
    79                         std::ostringstream stream;
    80                         stream << "No alternatives with satisfiable assertions for " << expr << "\n";
    81                         for ( const auto& err : errors ) {
    82                                 stream << err;
    83                         }
    84                         SemanticError( expr->location, stream.str() );
    85                 }
    86 
    87                 // reset candidates
    88                 candidates = std::move( satisfied );
    89         }
    90 
    91         if ( mode.prune ) {
    92                 // trim candidates to single best one
    93                 auto oldsize = candidates.size();
    94                 PRINT(
    95                         std::cerr << "alternatives before prune:" << std::endl;
    96                         print( std::cerr, candidates );
    97                 )
    98 
    99                 CandidateList pruned = pruneCandidates( candidates );
    100                 if ( mode.failFast && pruned.empty() ) {
    101                         std::ostringstream stream;
    102                         CandidateList winners;
    103                        
    104                         #warning unimplemented
    105                         assert(false);
    106                 }
    107         }
    108 
    10923        #warning unimplemented
     24        (void)expr; (void)mode;
    11025        assert(false);
    111 }
    112 
    113 std::vector< CandidateFinder > CandidateFinder::findSubExprs(
    114         const std::vector< ast::ptr< ast::Expr > > & xs
    115 ) {
    116         std::vector< CandidateFinder > out;
    117 
    118         for ( const auto & x : xs ) {
    119                 out.emplace_back( symtab, env );
    120                 out.back().find( x, ResolvMode::withAdjustment() );
    121                
    122                 PRINT(
    123                         std::cerr << "findSubExprs" << std::endl;
    124                         print( std::cerr, out.back().candidates );
    125                 )
    126         }
    127 
    128         return out;
    12926}
    13027
Note: See TracChangeset for help on using the changeset viewer.