| 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 | //
|
|---|
| 7 | // CandidateFinder.cpp --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Aaron B. Moss
|
|---|
| 10 | // Created On : Wed Jun 5 14:30:00 2019
|
|---|
| 11 | // Last Modified By : Aaron B. Moss
|
|---|
| 12 | // Last Modified On : Wed Jun 5 14:30:00 2019
|
|---|
| 13 | // Update Count : 1
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "CandidateFinder.hpp"
|
|---|
| 17 |
|
|---|
| 18 | #include <sstream>
|
|---|
| 19 |
|
|---|
| 20 | #include "Candidate.hpp"
|
|---|
| 21 | #include "CompilationState.h"
|
|---|
| 22 | #include "SatisfyAssertions.hpp"
|
|---|
| 23 | #include "AST/Expr.hpp"
|
|---|
| 24 | #include "AST/Node.hpp"
|
|---|
| 25 | #include "AST/Pass.hpp"
|
|---|
| 26 |
|
|---|
| 27 | #define PRINT( text ) if ( resolvep ) { text }
|
|---|
| 28 |
|
|---|
| 29 | namespace ResolvExpr {
|
|---|
| 30 |
|
|---|
| 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 |
|
|---|
| 59 | void 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 |
|
|---|
| 109 | #warning unimplemented
|
|---|
| 110 | 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;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | } // namespace ResolvExpr
|
|---|
| 132 |
|
|---|
| 133 | // Local Variables: //
|
|---|
| 134 | // tab-width: 4 //
|
|---|
| 135 | // mode: c++ //
|
|---|
| 136 | // compile-command: "make install" //
|
|---|
| 137 | // End: //
|
|---|