| 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 | // Candidate.hpp --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Aaron B. Moss
 | 
|---|
| 10 | // Created On       : Wed Jun 5 14:30:00 2019
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Wed Jun 12 14:15:00 2019
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <iosfwd>
 | 
|---|
| 19 | #include <memory>        // for shared_ptr
 | 
|---|
| 20 | #include <vector>
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Cost.h"
 | 
|---|
| 23 | #include "AST/Node.hpp"
 | 
|---|
| 24 | #include "AST/TypeEnvironment.hpp"
 | 
|---|
| 25 | #include "Common/Indenter.h"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace ast {
 | 
|---|
| 28 |         class Expr;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |         /// A list of unresolved assertions
 | 
|---|
| 31 |         using AssertionList = std::vector<AssertionSet::value_type>;
 | 
|---|
| 32 | 
 | 
|---|
| 33 |         /// Convenience to merge AssertionList into AssertionSet
 | 
|---|
| 34 |         static inline void mergeAssertionSet( AssertionSet & dst, const AssertionList & src ) {
 | 
|---|
| 35 |                 for ( const auto & s : src ) { dst.emplace( s ); }
 | 
|---|
| 36 |         }
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | namespace ResolvExpr {
 | 
|---|
| 40 | 
 | 
|---|
| 41 | /// One option for resolution of an expression
 | 
|---|
| 42 | struct Candidate {
 | 
|---|
| 43 |         ast::ptr<ast::Expr> expr;  ///< Satisfying expression
 | 
|---|
| 44 |         Cost cost;                 ///< Cost of the whole expression
 | 
|---|
| 45 |         Cost cvtCost;              ///< Cost of conversions to satisfying expression
 | 
|---|
| 46 |         ast::TypeEnvironment env;  ///< Containing type environment
 | 
|---|
| 47 |         ast::OpenVarSet open;      ///< Open variables for environment
 | 
|---|
| 48 |         ast::AssertionList need;   ///< Assertions which need to be resolved
 | 
|---|
| 49 | 
 | 
|---|
| 50 |         Candidate() : expr(), cost( Cost::zero ), cvtCost( Cost::zero ), env(), open(), need() {}
 | 
|---|
| 51 | 
 | 
|---|
| 52 |         Candidate( const ast::Expr * x, const ast::TypeEnvironment & e )
 | 
|---|
| 53 |         : expr( x ), cost( Cost::zero ), cvtCost( Cost::zero ), env( e ), open(), need() {}
 | 
|---|
| 54 | 
 | 
|---|
| 55 |         Candidate( const Candidate & o, const ast::Expr * x )
 | 
|---|
| 56 |         : expr( x ), cost( o.cost ), cvtCost( Cost::zero ), env( o.env ), open( o.open ),
 | 
|---|
| 57 |           need( o.need ) {}
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         Candidate(
 | 
|---|
| 60 |                 const ast::Expr * x, const ast::TypeEnvironment & e, const ast::OpenVarSet & o, 
 | 
|---|
| 61 |                 const ast::AssertionSet & n, const Cost & c, const Cost & cvt = Cost::zero )
 | 
|---|
| 62 |         : expr( x ), cost( c ), cvtCost( cvt ), env( e ), open( o ), need( n.begin(), n.end() ) {}
 | 
|---|
| 63 | 
 | 
|---|
| 64 |         Candidate(
 | 
|---|
| 65 |                 const ast::Expr * x, ast::TypeEnvironment && e, ast::OpenVarSet && o,
 | 
|---|
| 66 |                 ast::AssertionSet && n, const Cost & c, const Cost & cvt = Cost::zero )
 | 
|---|
| 67 |         : expr( x ), cost( c ), cvtCost( cvt ), env( std::move( e ) ), open( std::move( o ) ),
 | 
|---|
| 68 |           need( n.begin(), n.end() ) {}
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | /// Shared reference to a candidate
 | 
|---|
| 72 | using CandidateRef = std::shared_ptr< Candidate >;
 | 
|---|
| 73 | 
 | 
|---|
| 74 | /// List of candidates
 | 
|---|
| 75 | using CandidateList = std::vector< CandidateRef >;
 | 
|---|
| 76 | 
 | 
|---|
| 77 | /// Splice src after dst, clearing src
 | 
|---|
| 78 | static inline void splice( CandidateList & dst, CandidateList & src ) {
 | 
|---|
| 79 |         dst.reserve( dst.size() + src.size() );
 | 
|---|
| 80 |         for ( CandidateRef & r : src ) { dst.emplace_back( std::move( r ) ); }
 | 
|---|
| 81 |         src.clear();
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /// Splice src before dst
 | 
|---|
| 85 | static inline void spliceBegin( CandidateList & dst, CandidateList & src ) {
 | 
|---|
| 86 |         splice( src, dst );
 | 
|---|
| 87 |         dst.swap( src );
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | /// Sum the cost of a list of candidates
 | 
|---|
| 91 | static inline Cost sumCost( const CandidateList & candidates ) {
 | 
|---|
| 92 |         Cost total = Cost::zero;
 | 
|---|
| 93 |         for ( const CandidateRef & r : candidates ) { total += r->cost; }
 | 
|---|
| 94 |         return total;
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | /// Holdover behaviour from old `findMinCost` -- xxx -- can maybe be eliminated?
 | 
|---|
| 98 | static inline void promoteCvtCost( CandidateList & candidates ) {
 | 
|---|
| 99 |         for ( CandidateRef & r : candidates ) {
 | 
|---|
| 100 |                 r->cost = r->cvtCost;
 | 
|---|
| 101 |         }
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | void print( std::ostream & os, const Candidate & cand, Indenter indent = {} );
 | 
|---|
| 105 | 
 | 
|---|
| 106 | void print( std::ostream & os, const CandidateList & cands, Indenter indent = {} );
 | 
|---|
| 107 | 
 | 
|---|
| 108 | } // namespace ResolvExpr
 | 
|---|
| 109 | 
 | 
|---|
| 110 | // Local Variables: //
 | 
|---|
| 111 | // tab-width: 4 //
 | 
|---|
| 112 | // mode: c++ //
 | 
|---|
| 113 | // compile-command: "make install" //
 | 
|---|
| 114 | // End: //
 | 
|---|