| [99d4584] | 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
 | 
|---|
| [cf32116] | 11 | // Last Modified By : Andrew Beach
 | 
|---|
 | 12 | // Last Modified On : Tue Oct  1 14:55:00 2019
 | 
|---|
 | 13 | // Update Count     : 2
 | 
|---|
| [99d4584] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "CandidateFinder.hpp"
 | 
|---|
 | 17 | 
 | 
|---|
| [432ce7a] | 18 | #include <deque>
 | 
|---|
| [4b7cce6] | 19 | #include <iterator>               // for back_inserter
 | 
|---|
| [396037d] | 20 | #include <sstream>
 | 
|---|
| [d57e349] | 21 | #include <string>
 | 
|---|
 | 22 | #include <unordered_map>
 | 
|---|
| [432ce7a] | 23 | #include <vector>
 | 
|---|
| [396037d] | 24 | 
 | 
|---|
 | 25 | #include "Candidate.hpp"
 | 
|---|
 | 26 | #include "CompilationState.h"
 | 
|---|
| [d57e349] | 27 | #include "Cost.h"
 | 
|---|
| [432ce7a] | 28 | #include "ExplodedArg.hpp"
 | 
|---|
| [898ae07] | 29 | #include "RenameVars.h"           // for renameTyVars
 | 
|---|
| [d57e349] | 30 | #include "Resolver.h"
 | 
|---|
| [c8e4d2f8] | 31 | #include "ResolveTypeof.h"
 | 
|---|
| [396037d] | 32 | #include "SatisfyAssertions.hpp"
 | 
|---|
| [9d5089e] | 33 | #include "typeops.h"              // for adjustExprType, conversionCost, polyCost, specCost
 | 
|---|
| [4b7cce6] | 34 | #include "Unify.h"
 | 
|---|
| [99d4584] | 35 | #include "AST/Expr.hpp"
 | 
|---|
| [396037d] | 36 | #include "AST/Node.hpp"
 | 
|---|
 | 37 | #include "AST/Pass.hpp"
 | 
|---|
| [d57e349] | 38 | #include "AST/Print.hpp"
 | 
|---|
| [4b7cce6] | 39 | #include "AST/SymbolTable.hpp"
 | 
|---|
| [432ce7a] | 40 | #include "AST/Type.hpp"
 | 
|---|
| [c1ed2ee] | 41 | #include "Common/utility.h"       // for move, copy
 | 
|---|
| [d57e349] | 42 | #include "SymTab/Mangler.h"
 | 
|---|
| [c8e4d2f8] | 43 | #include "SymTab/Validate.h"      // for validateType
 | 
|---|
| [432ce7a] | 44 | #include "Tuples/Tuples.h"        // for handleTupleAssignment
 | 
|---|
| [e5c3811] | 45 | #include "InitTweak/InitTweak.h"  // for getPointerBase
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | #include "Common/Stats/Counter.h"
 | 
|---|
| [396037d] | 48 | 
 | 
|---|
 | 49 | #define PRINT( text ) if ( resolvep ) { text }
 | 
|---|
| [99d4584] | 50 | 
 | 
|---|
 | 51 | namespace ResolvExpr {
 | 
|---|
 | 52 | 
 | 
|---|
| [9d5089e] | 53 | const ast::Expr * referenceToRvalueConversion( const ast::Expr * expr, Cost & cost ) {
 | 
|---|
 | 54 |         if ( expr->result.as< ast::ReferenceType >() ) {
 | 
|---|
 | 55 |                 // cast away reference from expr
 | 
|---|
 | 56 |                 cost.incReference();
 | 
|---|
| [b8524ca] | 57 |                 return new ast::CastExpr{ expr, expr->result->stripReferences() };
 | 
|---|
| [9d5089e] | 58 |         }
 | 
|---|
| [2890212] | 59 | 
 | 
|---|
| [9d5089e] | 60 |         return expr;
 | 
|---|
 | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | /// Unique identifier for matching expression resolutions to their requesting expression
 | 
|---|
 | 64 | UniqueId globalResnSlot = 0;
 | 
|---|
| [396037d] | 65 | 
 | 
|---|
| [2890212] | 66 | Cost computeConversionCost(
 | 
|---|
| [cf32116] | 67 |         const ast::Type * argType, const ast::Type * paramType, bool argIsLvalue,
 | 
|---|
 | 68 |         const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
 | 
|---|
| [b69233ac] | 69 | ) {
 | 
|---|
 | 70 |         PRINT(
 | 
|---|
 | 71 |                 std::cerr << std::endl << "converting ";
 | 
|---|
 | 72 |                 ast::print( std::cerr, argType, 2 );
 | 
|---|
 | 73 |                 std::cerr << std::endl << " to ";
 | 
|---|
 | 74 |                 ast::print( std::cerr, paramType, 2 );
 | 
|---|
 | 75 |                 std::cerr << std::endl << "environment is: ";
 | 
|---|
 | 76 |                 ast::print( std::cerr, env, 2 );
 | 
|---|
 | 77 |                 std::cerr << std::endl;
 | 
|---|
 | 78 |         )
 | 
|---|
| [cf32116] | 79 |         Cost convCost = conversionCost( argType, paramType, argIsLvalue, symtab, env );
 | 
|---|
| [b69233ac] | 80 |         PRINT(
 | 
|---|
 | 81 |                 std::cerr << std::endl << "cost is " << convCost << std::endl;
 | 
|---|
 | 82 |         )
 | 
|---|
 | 83 |         if ( convCost == Cost::infinity ) return convCost;
 | 
|---|
 | 84 |         convCost.incPoly( polyCost( paramType, symtab, env ) + polyCost( argType, symtab, env ) );
 | 
|---|
 | 85 |         PRINT(
 | 
|---|
 | 86 |                 std::cerr << "cost with polycost is " << convCost << std::endl;
 | 
|---|
 | 87 |         )
 | 
|---|
 | 88 |         return convCost;
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [9d5089e] | 91 | namespace {
 | 
|---|
| [432ce7a] | 92 |         /// First index is which argument, second is which alternative, third is which exploded element
 | 
|---|
 | 93 |         using ExplodedArgs_new = std::deque< std::vector< ExplodedArg > >;
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |         /// Returns a list of alternatives with the minimum cost in the given list
 | 
|---|
 | 96 |         CandidateList findMinCost( const CandidateList & candidates ) {
 | 
|---|
 | 97 |                 CandidateList out;
 | 
|---|
 | 98 |                 Cost minCost = Cost::infinity;
 | 
|---|
 | 99 |                 for ( const CandidateRef & r : candidates ) {
 | 
|---|
 | 100 |                         if ( r->cost < minCost ) {
 | 
|---|
 | 101 |                                 minCost = r->cost;
 | 
|---|
 | 102 |                                 out.clear();
 | 
|---|
 | 103 |                                 out.emplace_back( r );
 | 
|---|
 | 104 |                         } else if ( r->cost == minCost ) {
 | 
|---|
 | 105 |                                 out.emplace_back( r );
 | 
|---|
 | 106 |                         }
 | 
|---|
 | 107 |                 }
 | 
|---|
 | 108 |                 return out;
 | 
|---|
 | 109 |         }
 | 
|---|
 | 110 | 
 | 
|---|
| [9d5089e] | 111 |         /// Computes conversion cost for a given expression to a given type
 | 
|---|
| [2890212] | 112 |         const ast::Expr * computeExpressionConversionCost(
 | 
|---|
 | 113 |                 const ast::Expr * arg, const ast::Type * paramType, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env, Cost & outCost
 | 
|---|
| [9d5089e] | 114 |         ) {
 | 
|---|
| [cf32116] | 115 |                 Cost convCost = computeConversionCost(
 | 
|---|
 | 116 |                                 arg->result, paramType, arg->get_lvalue(), symtab, env );
 | 
|---|
| [9d5089e] | 117 |                 outCost += convCost;
 | 
|---|
 | 118 | 
 | 
|---|
| [2890212] | 119 |                 // If there is a non-zero conversion cost, ignoring poly cost, then the expression requires
 | 
|---|
 | 120 |                 // conversion. Ignore poly cost for now, since this requires resolution of the cast to
 | 
|---|
| [9d5089e] | 121 |                 // infer parameters and this does not currently work for the reason stated below
 | 
|---|
 | 122 |                 Cost tmpCost = convCost;
 | 
|---|
 | 123 |                 tmpCost.incPoly( -tmpCost.get_polyCost() );
 | 
|---|
 | 124 |                 if ( tmpCost != Cost::zero ) {
 | 
|---|
 | 125 |                         ast::ptr< ast::Type > newType = paramType;
 | 
|---|
 | 126 |                         env.apply( newType );
 | 
|---|
| [b8524ca] | 127 |                         return new ast::CastExpr{ arg, newType };
 | 
|---|
| [9d5089e] | 128 | 
 | 
|---|
| [2890212] | 129 |                         // xxx - *should* be able to resolve this cast, but at the moment pointers are not
 | 
|---|
 | 130 |                         // castable to zero_t, but are implicitly convertible. This is clearly inconsistent,
 | 
|---|
| [9d5089e] | 131 |                         // once this is fixed it should be possible to resolve the cast.
 | 
|---|
| [2890212] | 132 |                         // xxx - this isn't working, it appears because type1 (parameter) is seen as widenable,
 | 
|---|
 | 133 |                         // but it shouldn't be because this makes the conversion from DT* to DT* since
 | 
|---|
| [9d5089e] | 134 |                         // commontype(zero_t, DT*) is DT*, rather than nothing
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |                         // CandidateFinder finder{ symtab, env };
 | 
|---|
 | 137 |                         // finder.find( arg, ResolvMode::withAdjustment() );
 | 
|---|
| [2890212] | 138 |                         // assertf( finder.candidates.size() > 0,
 | 
|---|
| [9d5089e] | 139 |                         //      "Somehow castable expression failed to find alternatives." );
 | 
|---|
| [2890212] | 140 |                         // assertf( finder.candidates.size() == 1,
 | 
|---|
| [9d5089e] | 141 |                         //      "Somehow got multiple alternatives for known cast expression." );
 | 
|---|
 | 142 |                         // return finder.candidates.front()->expr;
 | 
|---|
 | 143 |                 }
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 |                 return arg;
 | 
|---|
 | 146 |         }
 | 
|---|
 | 147 | 
 | 
|---|
| [432ce7a] | 148 |         /// Computes conversion cost for a given candidate
 | 
|---|
| [2890212] | 149 |         Cost computeApplicationConversionCost(
 | 
|---|
 | 150 |                 CandidateRef cand, const ast::SymbolTable & symtab
 | 
|---|
| [432ce7a] | 151 |         ) {
 | 
|---|
| [9d5089e] | 152 |                 auto appExpr = cand->expr.strict_as< ast::ApplicationExpr >();
 | 
|---|
 | 153 |                 auto pointer = appExpr->func->result.strict_as< ast::PointerType >();
 | 
|---|
 | 154 |                 auto function = pointer->base.strict_as< ast::FunctionType >();
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 |                 Cost convCost = Cost::zero;
 | 
|---|
 | 157 |                 const auto & params = function->params;
 | 
|---|
 | 158 |                 auto param = params.begin();
 | 
|---|
 | 159 |                 auto & args = appExpr->args;
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 |                 for ( unsigned i = 0; i < args.size(); ++i ) {
 | 
|---|
 | 162 |                         const ast::Type * argType = args[i]->result;
 | 
|---|
 | 163 |                         PRINT(
 | 
|---|
 | 164 |                                 std::cerr << "arg expression:" << std::endl;
 | 
|---|
 | 165 |                                 ast::print( std::cerr, args[i], 2 );
 | 
|---|
 | 166 |                                 std::cerr << "--- results are" << std::endl;
 | 
|---|
 | 167 |                                 ast::print( std::cerr, argType, 2 );
 | 
|---|
 | 168 |                         )
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 |                         if ( param == params.end() ) {
 | 
|---|
 | 171 |                                 if ( function->isVarArgs ) {
 | 
|---|
 | 172 |                                         convCost.incUnsafe();
 | 
|---|
| [2890212] | 173 |                                         PRINT( std::cerr << "end of params with varargs function: inc unsafe: "
 | 
|---|
| [9d5089e] | 174 |                                                 << convCost << std::endl; ; )
 | 
|---|
 | 175 |                                         // convert reference-typed expressions into value-typed expressions
 | 
|---|
| [2890212] | 176 |                                         cand->expr = ast::mutate_field_index(
 | 
|---|
 | 177 |                                                 appExpr, &ast::ApplicationExpr::args, i,
 | 
|---|
| [9d5089e] | 178 |                                                 referenceToRvalueConversion( args[i], convCost ) );
 | 
|---|
 | 179 |                                         continue;
 | 
|---|
 | 180 |                                 } else return Cost::infinity;
 | 
|---|
 | 181 |                         }
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 |                         if ( auto def = args[i].as< ast::DefaultArgExpr >() ) {
 | 
|---|
 | 184 |                                 // Default arguments should be free - don't include conversion cost.
 | 
|---|
 | 185 |                                 // Unwrap them here because they are not relevant to the rest of the system
 | 
|---|
| [2890212] | 186 |                                 cand->expr = ast::mutate_field_index(
 | 
|---|
| [9d5089e] | 187 |                                         appExpr, &ast::ApplicationExpr::args, i, def->expr );
 | 
|---|
 | 188 |                                 ++param;
 | 
|---|
 | 189 |                                 continue;
 | 
|---|
 | 190 |                         }
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |                         // mark conversion cost and also specialization cost of param type
 | 
|---|
| [954c954] | 193 |                         // const ast::Type * paramType = (*param)->get_type();
 | 
|---|
| [2890212] | 194 |                         cand->expr = ast::mutate_field_index(
 | 
|---|
 | 195 |                                 appExpr, &ast::ApplicationExpr::args, i,
 | 
|---|
 | 196 |                                 computeExpressionConversionCost(
 | 
|---|
| [954c954] | 197 |                                         args[i], *param, symtab, cand->env, convCost ) );
 | 
|---|
 | 198 |                         convCost.decSpec( specCost( *param ) );
 | 
|---|
| [9d5089e] | 199 |                         ++param;  // can't be in for-loop update because of the continue
 | 
|---|
 | 200 |                 }
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |                 if ( param != params.end() ) return Cost::infinity;
 | 
|---|
 | 203 | 
 | 
|---|
| [2890212] | 204 |                 // specialization cost of return types can't be accounted for directly, it disables
 | 
|---|
| [9d5089e] | 205 |                 // otherwise-identical calls, like this example based on auto-newline in the I/O lib:
 | 
|---|
 | 206 |                 //
 | 
|---|
 | 207 |                 //   forall(otype OS) {
 | 
|---|
 | 208 |                 //     void ?|?(OS&, int);  // with newline
 | 
|---|
 | 209 |                 //     OS&  ?|?(OS&, int);  // no newline, always chosen due to more specialization
 | 
|---|
 | 210 |                 //   }
 | 
|---|
 | 211 | 
 | 
|---|
 | 212 |                 // mark type variable and specialization cost of forall clause
 | 
|---|
 | 213 |                 convCost.incVar( function->forall.size() );
 | 
|---|
| [3e5dd913] | 214 |                 convCost.decSpec( function->assertions.size() );
 | 
|---|
| [9d5089e] | 215 | 
 | 
|---|
 | 216 |                 return convCost;
 | 
|---|
 | 217 |         }
 | 
|---|
 | 218 | 
 | 
|---|
| [2890212] | 219 |         void makeUnifiableVars(
 | 
|---|
| [361bf01] | 220 |                 const ast::FunctionType * type, ast::OpenVarSet & unifiableVars,
 | 
|---|
| [2890212] | 221 |                 ast::AssertionSet & need
 | 
|---|
| [9d5089e] | 222 |         ) {
 | 
|---|
| [3e5dd913] | 223 |                 for ( auto & tyvar : type->forall ) {
 | 
|---|
 | 224 |                         unifiableVars[ *tyvar ] = ast::TypeDecl::Data{ tyvar->base };
 | 
|---|
 | 225 |                 }
 | 
|---|
 | 226 |                 for ( auto & assn : type->assertions ) {
 | 
|---|
 | 227 |                         need[ assn ].isUsed = true;
 | 
|---|
| [9d5089e] | 228 |                 }
 | 
|---|
 | 229 |         }
 | 
|---|
 | 230 | 
 | 
|---|
 | 231 |         /// Gets a default value from an initializer, nullptr if not present
 | 
|---|
 | 232 |         const ast::ConstantExpr * getDefaultValue( const ast::Init * init ) {
 | 
|---|
 | 233 |                 if ( auto si = dynamic_cast< const ast::SingleInit * >( init ) ) {
 | 
|---|
 | 234 |                         if ( auto ce = si->value.as< ast::CastExpr >() ) {
 | 
|---|
 | 235 |                                 return ce->arg.as< ast::ConstantExpr >();
 | 
|---|
 | 236 |                         } else {
 | 
|---|
 | 237 |                                 return si->value.as< ast::ConstantExpr >();
 | 
|---|
 | 238 |                         }
 | 
|---|
 | 239 |                 }
 | 
|---|
 | 240 |                 return nullptr;
 | 
|---|
 | 241 |         }
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 |         /// State to iteratively build a match of parameter expressions to arguments
 | 
|---|
 | 244 |         struct ArgPack {
 | 
|---|
 | 245 |                 std::size_t parent;          ///< Index of parent pack
 | 
|---|
 | 246 |                 ast::ptr< ast::Expr > expr;  ///< The argument stored here
 | 
|---|
 | 247 |                 Cost cost;                   ///< The cost of this argument
 | 
|---|
 | 248 |                 ast::TypeEnvironment env;    ///< Environment for this pack
 | 
|---|
 | 249 |                 ast::AssertionSet need;      ///< Assertions outstanding for this pack
 | 
|---|
 | 250 |                 ast::AssertionSet have;      ///< Assertions found for this pack
 | 
|---|
 | 251 |                 ast::OpenVarSet open;        ///< Open variables for this pack
 | 
|---|
 | 252 |                 unsigned nextArg;            ///< Index of next argument in arguments list
 | 
|---|
 | 253 |                 unsigned tupleStart;         ///< Number of tuples that start at this index
 | 
|---|
 | 254 |                 unsigned nextExpl;           ///< Index of next exploded element
 | 
|---|
 | 255 |                 unsigned explAlt;            ///< Index of alternative for nextExpl > 0
 | 
|---|
 | 256 | 
 | 
|---|
 | 257 |                 ArgPack()
 | 
|---|
| [2890212] | 258 |                 : parent( 0 ), expr(), cost( Cost::zero ), env(), need(), have(), open(), nextArg( 0 ),
 | 
|---|
| [9d5089e] | 259 |                   tupleStart( 0 ), nextExpl( 0 ), explAlt( 0 ) {}
 | 
|---|
| [2890212] | 260 | 
 | 
|---|
 | 261 |                 ArgPack(
 | 
|---|
 | 262 |                         const ast::TypeEnvironment & env, const ast::AssertionSet & need,
 | 
|---|
| [9d5089e] | 263 |                         const ast::AssertionSet & have, const ast::OpenVarSet & open )
 | 
|---|
| [2890212] | 264 |                 : parent( 0 ), expr(), cost( Cost::zero ), env( env ), need( need ), have( have ),
 | 
|---|
| [9d5089e] | 265 |                   open( open ), nextArg( 0 ), tupleStart( 0 ), nextExpl( 0 ), explAlt( 0 ) {}
 | 
|---|
| [2890212] | 266 | 
 | 
|---|
| [9d5089e] | 267 |                 ArgPack(
 | 
|---|
| [2890212] | 268 |                         std::size_t parent, const ast::Expr * expr, ast::TypeEnvironment && env,
 | 
|---|
 | 269 |                         ast::AssertionSet && need, ast::AssertionSet && have, ast::OpenVarSet && open,
 | 
|---|
 | 270 |                         unsigned nextArg, unsigned tupleStart = 0, Cost cost = Cost::zero,
 | 
|---|
| [9d5089e] | 271 |                         unsigned nextExpl = 0, unsigned explAlt = 0 )
 | 
|---|
 | 272 |                 : parent(parent), expr( expr ), cost( cost ), env( move( env ) ), need( move( need ) ),
 | 
|---|
 | 273 |                   have( move( have ) ), open( move( open ) ), nextArg( nextArg ), tupleStart( tupleStart ),
 | 
|---|
 | 274 |                   nextExpl( nextExpl ), explAlt( explAlt ) {}
 | 
|---|
| [2890212] | 275 | 
 | 
|---|
| [9d5089e] | 276 |                 ArgPack(
 | 
|---|
| [2890212] | 277 |                         const ArgPack & o, ast::TypeEnvironment && env, ast::AssertionSet && need,
 | 
|---|
| [9d5089e] | 278 |                         ast::AssertionSet && have, ast::OpenVarSet && open, unsigned nextArg, Cost added )
 | 
|---|
| [2890212] | 279 |                 : parent( o.parent ), expr( o.expr ), cost( o.cost + added ), env( move( env ) ),
 | 
|---|
 | 280 |                   need( move( need ) ), have( move( have ) ), open( move( open ) ), nextArg( nextArg ),
 | 
|---|
| [9d5089e] | 281 |                   tupleStart( o.tupleStart ), nextExpl( 0 ), explAlt( 0 ) {}
 | 
|---|
| [2890212] | 282 | 
 | 
|---|
| [9d5089e] | 283 |                 /// true if this pack is in the middle of an exploded argument
 | 
|---|
 | 284 |                 bool hasExpl() const { return nextExpl > 0; }
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 |                 /// Gets the list of exploded candidates for this pack
 | 
|---|
 | 287 |                 const ExplodedArg & getExpl( const ExplodedArgs_new & args ) const {
 | 
|---|
 | 288 |                         return args[ nextArg-1 ][ explAlt ];
 | 
|---|
 | 289 |                 }
 | 
|---|
| [2890212] | 290 | 
 | 
|---|
| [9d5089e] | 291 |                 /// Ends a tuple expression, consolidating the appropriate args
 | 
|---|
 | 292 |                 void endTuple( const std::vector< ArgPack > & packs ) {
 | 
|---|
 | 293 |                         // add all expressions in tuple to list, summing cost
 | 
|---|
 | 294 |                         std::deque< const ast::Expr * > exprs;
 | 
|---|
 | 295 |                         const ArgPack * pack = this;
 | 
|---|
 | 296 |                         if ( expr ) { exprs.emplace_front( expr ); }
 | 
|---|
 | 297 |                         while ( pack->tupleStart == 0 ) {
 | 
|---|
 | 298 |                                 pack = &packs[pack->parent];
 | 
|---|
 | 299 |                                 exprs.emplace_front( pack->expr );
 | 
|---|
 | 300 |                                 cost += pack->cost;
 | 
|---|
 | 301 |                         }
 | 
|---|
 | 302 |                         // reset pack to appropriate tuple
 | 
|---|
 | 303 |                         std::vector< ast::ptr< ast::Expr > > exprv( exprs.begin(), exprs.end() );
 | 
|---|
 | 304 |                         expr = new ast::TupleExpr{ expr->location, move( exprv ) };
 | 
|---|
 | 305 |                         tupleStart = pack->tupleStart - 1;
 | 
|---|
 | 306 |                         parent = pack->parent;
 | 
|---|
 | 307 |                 }
 | 
|---|
 | 308 |         };
 | 
|---|
 | 309 | 
 | 
|---|
 | 310 |         /// Instantiates an argument to match a parameter, returns false if no matching results left
 | 
|---|
| [2890212] | 311 |         bool instantiateArgument(
 | 
|---|
 | 312 |                 const ast::Type * paramType, const ast::Init * init, const ExplodedArgs_new & args,
 | 
|---|
 | 313 |                 std::vector< ArgPack > & results, std::size_t & genStart, const ast::SymbolTable & symtab,
 | 
|---|
 | 314 |                 unsigned nTuples = 0
 | 
|---|
| [9d5089e] | 315 |         ) {
 | 
|---|
 | 316 |                 if ( auto tupleType = dynamic_cast< const ast::TupleType * >( paramType ) ) {
 | 
|---|
 | 317 |                         // paramType is a TupleType -- group args into a TupleExpr
 | 
|---|
 | 318 |                         ++nTuples;
 | 
|---|
 | 319 |                         for ( const ast::Type * type : *tupleType ) {
 | 
|---|
 | 320 |                                 // xxx - dropping initializer changes behaviour from previous, but seems correct
 | 
|---|
 | 321 |                                 // ^^^ need to handle the case where a tuple has a default argument
 | 
|---|
| [2890212] | 322 |                                 if ( ! instantiateArgument(
 | 
|---|
| [9d5089e] | 323 |                                         type, nullptr, args, results, genStart, symtab, nTuples ) ) return false;
 | 
|---|
 | 324 |                                 nTuples = 0;
 | 
|---|
 | 325 |                         }
 | 
|---|
 | 326 |                         // re-constitute tuples for final generation
 | 
|---|
 | 327 |                         for ( auto i = genStart; i < results.size(); ++i ) {
 | 
|---|
 | 328 |                                 results[i].endTuple( results );
 | 
|---|
 | 329 |                         }
 | 
|---|
 | 330 |                         return true;
 | 
|---|
 | 331 |                 } else if ( const ast::TypeInstType * ttype = Tuples::isTtype( paramType ) ) {
 | 
|---|
 | 332 |                         // paramType is a ttype, consumes all remaining arguments
 | 
|---|
| [2890212] | 333 | 
 | 
|---|
| [9d5089e] | 334 |                         // completed tuples; will be spliced to end of results to finish
 | 
|---|
 | 335 |                         std::vector< ArgPack > finalResults{};
 | 
|---|
 | 336 | 
 | 
|---|
 | 337 |                         // iterate until all results completed
 | 
|---|
 | 338 |                         std::size_t genEnd;
 | 
|---|
 | 339 |                         ++nTuples;
 | 
|---|
 | 340 |                         do {
 | 
|---|
 | 341 |                                 genEnd = results.size();
 | 
|---|
 | 342 | 
 | 
|---|
 | 343 |                                 // add another argument to results
 | 
|---|
 | 344 |                                 for ( std::size_t i = genStart; i < genEnd; ++i ) {
 | 
|---|
 | 345 |                                         unsigned nextArg = results[i].nextArg;
 | 
|---|
| [2890212] | 346 | 
 | 
|---|
| [9d5089e] | 347 |                                         // use next element of exploded tuple if present
 | 
|---|
 | 348 |                                         if ( results[i].hasExpl() ) {
 | 
|---|
 | 349 |                                                 const ExplodedArg & expl = results[i].getExpl( args );
 | 
|---|
 | 350 | 
 | 
|---|
 | 351 |                                                 unsigned nextExpl = results[i].nextExpl + 1;
 | 
|---|
 | 352 |                                                 if ( nextExpl == expl.exprs.size() ) { nextExpl = 0; }
 | 
|---|
 | 353 | 
 | 
|---|
 | 354 |                                                 results.emplace_back(
 | 
|---|
 | 355 |                                                         i, expl.exprs[ results[i].nextExpl ], copy( results[i].env ),
 | 
|---|
| [2890212] | 356 |                                                         copy( results[i].need ), copy( results[i].have ),
 | 
|---|
| [9d5089e] | 357 |                                                         copy( results[i].open ), nextArg, nTuples, Cost::zero, nextExpl,
 | 
|---|
 | 358 |                                                         results[i].explAlt );
 | 
|---|
 | 359 | 
 | 
|---|
 | 360 |                                                 continue;
 | 
|---|
 | 361 |                                         }
 | 
|---|
 | 362 | 
 | 
|---|
 | 363 |                                         // finish result when out of arguments
 | 
|---|
 | 364 |                                         if ( nextArg >= args.size() ) {
 | 
|---|
 | 365 |                                                 ArgPack newResult{
 | 
|---|
 | 366 |                                                         results[i].env, results[i].need, results[i].have, results[i].open };
 | 
|---|
 | 367 |                                                 newResult.nextArg = nextArg;
 | 
|---|
 | 368 |                                                 const ast::Type * argType = nullptr;
 | 
|---|
 | 369 | 
 | 
|---|
 | 370 |                                                 if ( nTuples > 0 || ! results[i].expr ) {
 | 
|---|
 | 371 |                                                         // first iteration or no expression to clone,
 | 
|---|
 | 372 |                                                         // push empty tuple expression
 | 
|---|
 | 373 |                                                         newResult.parent = i;
 | 
|---|
| [417117e] | 374 |                                                         newResult.expr = new ast::TupleExpr{ CodeLocation{}, {} };
 | 
|---|
| [9d5089e] | 375 |                                                         argType = newResult.expr->result;
 | 
|---|
 | 376 |                                                 } else {
 | 
|---|
 | 377 |                                                         // clone result to collect tuple
 | 
|---|
 | 378 |                                                         newResult.parent = results[i].parent;
 | 
|---|
 | 379 |                                                         newResult.cost = results[i].cost;
 | 
|---|
 | 380 |                                                         newResult.tupleStart = results[i].tupleStart;
 | 
|---|
 | 381 |                                                         newResult.expr = results[i].expr;
 | 
|---|
 | 382 |                                                         argType = newResult.expr->result;
 | 
|---|
 | 383 | 
 | 
|---|
 | 384 |                                                         if ( results[i].tupleStart > 0 && Tuples::isTtype( argType ) ) {
 | 
|---|
 | 385 |                                                                 // the case where a ttype value is passed directly is special,
 | 
|---|
 | 386 |                                                                 // e.g. for argument forwarding purposes
 | 
|---|
 | 387 |                                                                 // xxx - what if passing multiple arguments, last of which is
 | 
|---|
 | 388 |                                                                 //       ttype?
 | 
|---|
 | 389 |                                                                 // xxx - what would happen if unify was changed so that unifying
 | 
|---|
 | 390 |                                                                 //       tuple
 | 
|---|
 | 391 |                                                                 // types flattened both before unifying lists? then pass in
 | 
|---|
 | 392 |                                                                 // TupleType (ttype) below.
 | 
|---|
 | 393 |                                                                 --newResult.tupleStart;
 | 
|---|
 | 394 |                                                         } else {
 | 
|---|
 | 395 |                                                                 // collapse leftover arguments into tuple
 | 
|---|
 | 396 |                                                                 newResult.endTuple( results );
 | 
|---|
 | 397 |                                                                 argType = newResult.expr->result;
 | 
|---|
 | 398 |                                                         }
 | 
|---|
 | 399 |                                                 }
 | 
|---|
 | 400 | 
 | 
|---|
 | 401 |                                                 // check unification for ttype before adding to final
 | 
|---|
| [2890212] | 402 |                                                 if (
 | 
|---|
 | 403 |                                                         unify(
 | 
|---|
| [9d5089e] | 404 |                                                                 ttype, argType, newResult.env, newResult.need, newResult.have,
 | 
|---|
| [2890212] | 405 |                                                                 newResult.open, symtab )
 | 
|---|
| [9d5089e] | 406 |                                                 ) {
 | 
|---|
 | 407 |                                                         finalResults.emplace_back( move( newResult ) );
 | 
|---|
 | 408 |                                                 }
 | 
|---|
 | 409 | 
 | 
|---|
 | 410 |                                                 continue;
 | 
|---|
 | 411 |                                         }
 | 
|---|
 | 412 | 
 | 
|---|
 | 413 |                                         // add each possible next argument
 | 
|---|
 | 414 |                                         for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) {
 | 
|---|
 | 415 |                                                 const ExplodedArg & expl = args[nextArg][j];
 | 
|---|
 | 416 | 
 | 
|---|
 | 417 |                                                 // fresh copies of parent parameters for this iteration
 | 
|---|
 | 418 |                                                 ast::TypeEnvironment env = results[i].env;
 | 
|---|
 | 419 |                                                 ast::OpenVarSet open = results[i].open;
 | 
|---|
 | 420 | 
 | 
|---|
 | 421 |                                                 env.addActual( expl.env, open );
 | 
|---|
 | 422 | 
 | 
|---|
 | 423 |                                                 // skip empty tuple arguments by (nearly) cloning parent into next gen
 | 
|---|
 | 424 |                                                 if ( expl.exprs.empty() ) {
 | 
|---|
 | 425 |                                                         results.emplace_back(
 | 
|---|
| [2890212] | 426 |                                                                 results[i], move( env ), copy( results[i].need ),
 | 
|---|
| [9d5089e] | 427 |                                                                 copy( results[i].have ), move( open ), nextArg + 1, expl.cost );
 | 
|---|
| [2890212] | 428 | 
 | 
|---|
| [9d5089e] | 429 |                                                         continue;
 | 
|---|
 | 430 |                                                 }
 | 
|---|
 | 431 | 
 | 
|---|
 | 432 |                                                 // add new result
 | 
|---|
 | 433 |                                                 results.emplace_back(
 | 
|---|
| [2890212] | 434 |                                                         i, expl.exprs.front(), move( env ), copy( results[i].need ),
 | 
|---|
 | 435 |                                                         copy( results[i].have ), move( open ), nextArg + 1, nTuples,
 | 
|---|
| [9d5089e] | 436 |                                                         expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
 | 
|---|
 | 437 |                                         }
 | 
|---|
 | 438 |                                 }
 | 
|---|
 | 439 | 
 | 
|---|
 | 440 |                                 // reset for next round
 | 
|---|
 | 441 |                                 genStart = genEnd;
 | 
|---|
 | 442 |                                 nTuples = 0;
 | 
|---|
 | 443 |                         } while ( genEnd != results.size() );
 | 
|---|
 | 444 | 
 | 
|---|
 | 445 |                         // splice final results onto results
 | 
|---|
 | 446 |                         for ( std::size_t i = 0; i < finalResults.size(); ++i ) {
 | 
|---|
 | 447 |                                 results.emplace_back( move( finalResults[i] ) );
 | 
|---|
 | 448 |                         }
 | 
|---|
 | 449 |                         return ! finalResults.empty();
 | 
|---|
 | 450 |                 }
 | 
|---|
 | 451 | 
 | 
|---|
 | 452 |                 // iterate each current subresult
 | 
|---|
 | 453 |                 std::size_t genEnd = results.size();
 | 
|---|
 | 454 |                 for ( std::size_t i = genStart; i < genEnd; ++i ) {
 | 
|---|
 | 455 |                         unsigned nextArg = results[i].nextArg;
 | 
|---|
 | 456 | 
 | 
|---|
 | 457 |                         // use remainder of exploded tuple if present
 | 
|---|
 | 458 |                         if ( results[i].hasExpl() ) {
 | 
|---|
 | 459 |                                 const ExplodedArg & expl = results[i].getExpl( args );
 | 
|---|
 | 460 |                                 const ast::Expr * expr = expl.exprs[ results[i].nextExpl ];
 | 
|---|
 | 461 | 
 | 
|---|
 | 462 |                                 ast::TypeEnvironment env = results[i].env;
 | 
|---|
 | 463 |                                 ast::AssertionSet need = results[i].need, have = results[i].have;
 | 
|---|
 | 464 |                                 ast::OpenVarSet open = results[i].open;
 | 
|---|
 | 465 | 
 | 
|---|
 | 466 |                                 const ast::Type * argType = expr->result;
 | 
|---|
 | 467 | 
 | 
|---|
 | 468 |                                 PRINT(
 | 
|---|
 | 469 |                                         std::cerr << "param type is ";
 | 
|---|
 | 470 |                                         ast::print( std::cerr, paramType );
 | 
|---|
 | 471 |                                         std::cerr << std::endl << "arg type is ";
 | 
|---|
 | 472 |                                         ast::print( std::cerr, argType );
 | 
|---|
 | 473 |                                         std::cerr << std::endl;
 | 
|---|
 | 474 |                                 )
 | 
|---|
 | 475 | 
 | 
|---|
 | 476 |                                 if ( unify( paramType, argType, env, need, have, open, symtab ) ) {
 | 
|---|
 | 477 |                                         unsigned nextExpl = results[i].nextExpl + 1;
 | 
|---|
 | 478 |                                         if ( nextExpl == expl.exprs.size() ) { nextExpl = 0; }
 | 
|---|
 | 479 | 
 | 
|---|
 | 480 |                                         results.emplace_back(
 | 
|---|
| [2890212] | 481 |                                                 i, expr, move( env ), move( need ), move( have ), move( open ), nextArg,
 | 
|---|
| [9d5089e] | 482 |                                                 nTuples, Cost::zero, nextExpl, results[i].explAlt );
 | 
|---|
 | 483 |                                 }
 | 
|---|
 | 484 | 
 | 
|---|
 | 485 |                                 continue;
 | 
|---|
 | 486 |                         }
 | 
|---|
 | 487 | 
 | 
|---|
 | 488 |                         // use default initializers if out of arguments
 | 
|---|
 | 489 |                         if ( nextArg >= args.size() ) {
 | 
|---|
 | 490 |                                 if ( const ast::ConstantExpr * cnst = getDefaultValue( init ) ) {
 | 
|---|
 | 491 |                                         ast::TypeEnvironment env = results[i].env;
 | 
|---|
 | 492 |                                         ast::AssertionSet need = results[i].need, have = results[i].have;
 | 
|---|
 | 493 |                                         ast::OpenVarSet open = results[i].open;
 | 
|---|
 | 494 | 
 | 
|---|
 | 495 |                                         if ( unify( paramType, cnst->result, env, need, have, open, symtab ) ) {
 | 
|---|
 | 496 |                                                 results.emplace_back(
 | 
|---|
| [2890212] | 497 |                                                         i, new ast::DefaultArgExpr{ cnst->location, cnst }, move( env ),
 | 
|---|
| [9d5089e] | 498 |                                                         move( need ), move( have ), move( open ), nextArg, nTuples );
 | 
|---|
 | 499 |                                         }
 | 
|---|
 | 500 |                                 }
 | 
|---|
 | 501 | 
 | 
|---|
 | 502 |                                 continue;
 | 
|---|
 | 503 |                         }
 | 
|---|
 | 504 | 
 | 
|---|
 | 505 |                         // Check each possible next argument
 | 
|---|
 | 506 |                         for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) {
 | 
|---|
 | 507 |                                 const ExplodedArg & expl = args[nextArg][j];
 | 
|---|
 | 508 | 
 | 
|---|
 | 509 |                                 // fresh copies of parent parameters for this iteration
 | 
|---|
 | 510 |                                 ast::TypeEnvironment env = results[i].env;
 | 
|---|
 | 511 |                                 ast::AssertionSet need = results[i].need, have = results[i].have;
 | 
|---|
 | 512 |                                 ast::OpenVarSet open = results[i].open;
 | 
|---|
 | 513 | 
 | 
|---|
 | 514 |                                 env.addActual( expl.env, open );
 | 
|---|
 | 515 | 
 | 
|---|
 | 516 |                                 // skip empty tuple arguments by (nearly) cloning parent into next gen
 | 
|---|
 | 517 |                                 if ( expl.exprs.empty() ) {
 | 
|---|
 | 518 |                                         results.emplace_back(
 | 
|---|
| [2890212] | 519 |                                                 results[i], move( env ), move( need ), move( have ), move( open ),
 | 
|---|
| [9d5089e] | 520 |                                                 nextArg + 1, expl.cost );
 | 
|---|
| [2890212] | 521 | 
 | 
|---|
| [9d5089e] | 522 |                                         continue;
 | 
|---|
 | 523 |                                 }
 | 
|---|
 | 524 | 
 | 
|---|
 | 525 |                                 // consider only first exploded arg
 | 
|---|
 | 526 |                                 const ast::Expr * expr = expl.exprs.front();
 | 
|---|
 | 527 |                                 const ast::Type * argType = expr->result;
 | 
|---|
 | 528 | 
 | 
|---|
 | 529 |                                 PRINT(
 | 
|---|
 | 530 |                                         std::cerr << "param type is ";
 | 
|---|
 | 531 |                                         ast::print( std::cerr, paramType );
 | 
|---|
 | 532 |                                         std::cerr << std::endl << "arg type is ";
 | 
|---|
 | 533 |                                         ast::print( std::cerr, argType );
 | 
|---|
 | 534 |                                         std::cerr << std::endl;
 | 
|---|
 | 535 |                                 )
 | 
|---|
 | 536 | 
 | 
|---|
 | 537 |                                 // attempt to unify types
 | 
|---|
 | 538 |                                 if ( unify( paramType, argType, env, need, have, open, symtab ) ) {
 | 
|---|
 | 539 |                                         // add new result
 | 
|---|
 | 540 |                                         results.emplace_back(
 | 
|---|
| [2890212] | 541 |                                                 i, expr, move( env ), move( need ), move( have ), move( open ),
 | 
|---|
| [9d5089e] | 542 |                                                 nextArg + 1, nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
 | 
|---|
 | 543 |                                 }
 | 
|---|
 | 544 |                         }
 | 
|---|
 | 545 |                 }
 | 
|---|
 | 546 | 
 | 
|---|
 | 547 |                 // reset for next parameter
 | 
|---|
 | 548 |                 genStart = genEnd;
 | 
|---|
 | 549 | 
 | 
|---|
| [e0e9a0b] | 550 |                 return genEnd != results.size();  // were any new results added?
 | 
|---|
| [432ce7a] | 551 |         }
 | 
|---|
 | 552 | 
 | 
|---|
| [c8e4d2f8] | 553 |         /// Generate a cast expression from `arg` to `toType`
 | 
|---|
| [2890212] | 554 |         const ast::Expr * restructureCast(
 | 
|---|
| [17a0ede2] | 555 |                 ast::ptr< ast::Expr > & arg, const ast::Type * toType, ast::GeneratedFlag isGenerated = ast::GeneratedCast
 | 
|---|
| [898ae07] | 556 |         ) {
 | 
|---|
| [2890212] | 557 |                 if (
 | 
|---|
 | 558 |                         arg->result->size() > 1
 | 
|---|
 | 559 |                         && ! toType->isVoid()
 | 
|---|
 | 560 |                         && ! dynamic_cast< const ast::ReferenceType * >( toType )
 | 
|---|
| [898ae07] | 561 |                 ) {
 | 
|---|
| [2890212] | 562 |                         // Argument is a tuple and the target type is neither void nor a reference. Cast each
 | 
|---|
 | 563 |                         // member of the tuple to its corresponding target type, producing the tuple of those
 | 
|---|
 | 564 |                         // cast expressions. If there are more components of the tuple than components in the
 | 
|---|
 | 565 |                         // target type, then excess components do not come out in the result expression (but
 | 
|---|
| [898ae07] | 566 |                         // UniqueExpr ensures that the side effects will still be produced)
 | 
|---|
 | 567 |                         if ( Tuples::maybeImpureIgnoreUnique( arg ) ) {
 | 
|---|
| [2890212] | 568 |                                 // expressions which may contain side effects require a single unique instance of
 | 
|---|
| [898ae07] | 569 |                                 // the expression
 | 
|---|
 | 570 |                                 arg = new ast::UniqueExpr{ arg->location, arg };
 | 
|---|
 | 571 |                         }
 | 
|---|
 | 572 |                         std::vector< ast::ptr< ast::Expr > > components;
 | 
|---|
 | 573 |                         for ( unsigned i = 0; i < toType->size(); ++i ) {
 | 
|---|
 | 574 |                                 // cast each component
 | 
|---|
 | 575 |                                 ast::ptr< ast::Expr > idx = new ast::TupleIndexExpr{ arg->location, arg, i };
 | 
|---|
| [2890212] | 576 |                                 components.emplace_back(
 | 
|---|
| [898ae07] | 577 |                                         restructureCast( idx, toType->getComponent( i ), isGenerated ) );
 | 
|---|
 | 578 |                         }
 | 
|---|
 | 579 |                         return new ast::TupleExpr{ arg->location, move( components ) };
 | 
|---|
 | 580 |                 } else {
 | 
|---|
 | 581 |                         // handle normally
 | 
|---|
 | 582 |                         return new ast::CastExpr{ arg->location, arg, toType, isGenerated };
 | 
|---|
 | 583 |                 }
 | 
|---|
 | 584 |         }
 | 
|---|
 | 585 | 
 | 
|---|
 | 586 |         /// Gets the name from an untyped member expression (must be NameExpr)
 | 
|---|
 | 587 |         const std::string & getMemberName( const ast::UntypedMemberExpr * memberExpr ) {
 | 
|---|
 | 588 |                 if ( memberExpr->member.as< ast::ConstantExpr >() ) {
 | 
|---|
 | 589 |                         SemanticError( memberExpr, "Indexed access to struct fields unsupported: " );
 | 
|---|
 | 590 |                 }
 | 
|---|
 | 591 | 
 | 
|---|
 | 592 |                 return memberExpr->member.strict_as< ast::NameExpr >()->name;
 | 
|---|
| [c8e4d2f8] | 593 |         }
 | 
|---|
 | 594 | 
 | 
|---|
| [396037d] | 595 |         /// Actually visits expressions to find their candidate interpretations
 | 
|---|
| [9ea38de] | 596 |         class Finder final : public ast::WithShortCircuiting {
 | 
|---|
| [396037d] | 597 |                 const ast::SymbolTable & symtab;
 | 
|---|
| [9ea38de] | 598 |         public:
 | 
|---|
| [943bfad] | 599 |                 // static size_t traceId;
 | 
|---|
| [9ea38de] | 600 |                 CandidateFinder & selfFinder;
 | 
|---|
| [396037d] | 601 |                 CandidateList & candidates;
 | 
|---|
 | 602 |                 const ast::TypeEnvironment & tenv;
 | 
|---|
 | 603 |                 ast::ptr< ast::Type > & targetType;
 | 
|---|
 | 604 | 
 | 
|---|
| [71d6bd8] | 605 |                 enum Errors {
 | 
|---|
 | 606 |                         NotFound,
 | 
|---|
 | 607 |                         NoMatch,
 | 
|---|
 | 608 |                         ArgsToFew,
 | 
|---|
 | 609 |                         ArgsToMany,
 | 
|---|
 | 610 |                         RetsToFew,
 | 
|---|
 | 611 |                         RetsToMany,
 | 
|---|
 | 612 |                         NoReason
 | 
|---|
 | 613 |                 };
 | 
|---|
 | 614 | 
 | 
|---|
 | 615 |                 struct {
 | 
|---|
 | 616 |                         Errors code = NotFound;
 | 
|---|
 | 617 |                 } reason;
 | 
|---|
 | 618 | 
 | 
|---|
| [396037d] | 619 |                 Finder( CandidateFinder & f )
 | 
|---|
| [2890212] | 620 |                 : symtab( f.localSyms ), selfFinder( f ), candidates( f.candidates ), tenv( f.env ),
 | 
|---|
| [396037d] | 621 |                   targetType( f.targetType ) {}
 | 
|---|
| [2890212] | 622 | 
 | 
|---|
| [4b7cce6] | 623 |                 void previsit( const ast::Node * ) { visit_children = false; }
 | 
|---|
 | 624 | 
 | 
|---|
 | 625 |                 /// Convenience to add candidate to list
 | 
|---|
 | 626 |                 template<typename... Args>
 | 
|---|
 | 627 |                 void addCandidate( Args &&... args ) {
 | 
|---|
 | 628 |                         candidates.emplace_back( new Candidate{ std::forward<Args>( args )... } );
 | 
|---|
| [71d6bd8] | 629 |                         reason.code = NoReason;
 | 
|---|
| [4b7cce6] | 630 |                 }
 | 
|---|
 | 631 | 
 | 
|---|
 | 632 |                 void postvisit( const ast::ApplicationExpr * applicationExpr ) {
 | 
|---|
 | 633 |                         addCandidate( applicationExpr, tenv );
 | 
|---|
 | 634 |                 }
 | 
|---|
 | 635 | 
 | 
|---|
| [9d5089e] | 636 |                 /// Set up candidate assertions for inference
 | 
|---|
 | 637 |                 void inferParameters( CandidateRef & newCand, CandidateList & out ) {
 | 
|---|
 | 638 |                         // Set need bindings for any unbound assertions
 | 
|---|
 | 639 |                         UniqueId crntResnSlot = 0; // matching ID for this expression's assertions
 | 
|---|
 | 640 |                         for ( auto & assn : newCand->need ) {
 | 
|---|
 | 641 |                                 // skip already-matched assertions
 | 
|---|
 | 642 |                                 if ( assn.second.resnSlot != 0 ) continue;
 | 
|---|
 | 643 |                                 // assign slot for expression if needed
 | 
|---|
 | 644 |                                 if ( crntResnSlot == 0 ) { crntResnSlot = ++globalResnSlot; }
 | 
|---|
 | 645 |                                 // fix slot to assertion
 | 
|---|
 | 646 |                                 assn.second.resnSlot = crntResnSlot;
 | 
|---|
 | 647 |                         }
 | 
|---|
 | 648 |                         // pair slot to expression
 | 
|---|
 | 649 |                         if ( crntResnSlot != 0 ) {
 | 
|---|
 | 650 |                                 newCand->expr.get_and_mutate()->inferred.resnSlots().emplace_back( crntResnSlot );
 | 
|---|
 | 651 |                         }
 | 
|---|
 | 652 | 
 | 
|---|
 | 653 |                         // add to output list; assertion satisfaction will occur later
 | 
|---|
 | 654 |                         out.emplace_back( newCand );
 | 
|---|
 | 655 |                 }
 | 
|---|
 | 656 | 
 | 
|---|
 | 657 |                 /// Completes a function candidate with arguments located
 | 
|---|
| [2890212] | 658 |                 void validateFunctionCandidate(
 | 
|---|
 | 659 |                         const CandidateRef & func, ArgPack & result, const std::vector< ArgPack > & results,
 | 
|---|
 | 660 |                         CandidateList & out
 | 
|---|
| [9d5089e] | 661 |                 ) {
 | 
|---|
| [2890212] | 662 |                         ast::ApplicationExpr * appExpr =
 | 
|---|
| [9d5089e] | 663 |                                 new ast::ApplicationExpr{ func->expr->location, func->expr };
 | 
|---|
 | 664 |                         // sum cost and accumulate arguments
 | 
|---|
 | 665 |                         std::deque< const ast::Expr * > args;
 | 
|---|
 | 666 |                         Cost cost = func->cost;
 | 
|---|
 | 667 |                         const ArgPack * pack = &result;
 | 
|---|
 | 668 |                         while ( pack->expr ) {
 | 
|---|
 | 669 |                                 args.emplace_front( pack->expr );
 | 
|---|
 | 670 |                                 cost += pack->cost;
 | 
|---|
 | 671 |                                 pack = &results[pack->parent];
 | 
|---|
 | 672 |                         }
 | 
|---|
 | 673 |                         std::vector< ast::ptr< ast::Expr > > vargs( args.begin(), args.end() );
 | 
|---|
 | 674 |                         appExpr->args = move( vargs );
 | 
|---|
 | 675 |                         // build and validate new candidate
 | 
|---|
| [2890212] | 676 |                         auto newCand =
 | 
|---|
| [9d5089e] | 677 |                                 std::make_shared<Candidate>( appExpr, result.env, result.open, result.need, cost );
 | 
|---|
 | 678 |                         PRINT(
 | 
|---|
 | 679 |                                 std::cerr << "instantiate function success: " << appExpr << std::endl;
 | 
|---|
 | 680 |                                 std::cerr << "need assertions:" << std::endl;
 | 
|---|
 | 681 |                                 ast::print( std::cerr, result.need, 2 );
 | 
|---|
 | 682 |                         )
 | 
|---|
 | 683 |                         inferParameters( newCand, out );
 | 
|---|
 | 684 |                 }
 | 
|---|
 | 685 | 
 | 
|---|
| [432ce7a] | 686 |                 /// Builds a list of candidates for a function, storing them in out
 | 
|---|
 | 687 |                 void makeFunctionCandidates(
 | 
|---|
| [2890212] | 688 |                         const CandidateRef & func, const ast::FunctionType * funcType,
 | 
|---|
| [432ce7a] | 689 |                         const ExplodedArgs_new & args, CandidateList & out
 | 
|---|
 | 690 |                 ) {
 | 
|---|
| [9d5089e] | 691 |                         ast::OpenVarSet funcOpen;
 | 
|---|
 | 692 |                         ast::AssertionSet funcNeed, funcHave;
 | 
|---|
 | 693 |                         ast::TypeEnvironment funcEnv{ func->env };
 | 
|---|
 | 694 |                         makeUnifiableVars( funcType, funcOpen, funcNeed );
 | 
|---|
| [2890212] | 695 |                         // add all type variables as open variables now so that those not used in the
 | 
|---|
| [e0e9a0b] | 696 |                         // parameter list are still considered open
 | 
|---|
| [9d5089e] | 697 |                         funcEnv.add( funcType->forall );
 | 
|---|
 | 698 | 
 | 
|---|
 | 699 |                         if ( targetType && ! targetType->isVoid() && ! funcType->returns.empty() ) {
 | 
|---|
 | 700 |                                 // attempt to narrow based on expected target type
 | 
|---|
| [954c954] | 701 |                                 const ast::Type * returnType = funcType->returns.front();
 | 
|---|
| [2890212] | 702 |                                 if ( ! unify(
 | 
|---|
 | 703 |                                         returnType, targetType, funcEnv, funcNeed, funcHave, funcOpen, symtab )
 | 
|---|
| [9d5089e] | 704 |                                 ) {
 | 
|---|
 | 705 |                                         // unification failed, do not pursue this candidate
 | 
|---|
 | 706 |                                         return;
 | 
|---|
 | 707 |                                 }
 | 
|---|
 | 708 |                         }
 | 
|---|
 | 709 | 
 | 
|---|
 | 710 |                         // iteratively build matches, one parameter at a time
 | 
|---|
 | 711 |                         std::vector< ArgPack > results;
 | 
|---|
 | 712 |                         results.emplace_back( funcEnv, funcNeed, funcHave, funcOpen );
 | 
|---|
 | 713 |                         std::size_t genStart = 0;
 | 
|---|
 | 714 | 
 | 
|---|
| [954c954] | 715 |                         // xxx - how to handle default arg after change to ftype representation?
 | 
|---|
 | 716 |                         if (const ast::VariableExpr * varExpr = func->expr.as<ast::VariableExpr>()) {
 | 
|---|
 | 717 |                                 if (const ast::FunctionDecl * funcDecl = varExpr->var.as<ast::FunctionDecl>()) {
 | 
|---|
 | 718 |                                         // function may have default args only if directly calling by name
 | 
|---|
 | 719 |                                         // must use types on candidate however, due to RenameVars substitution
 | 
|---|
 | 720 |                                         auto nParams = funcType->params.size();
 | 
|---|
 | 721 | 
 | 
|---|
 | 722 |                                         for (size_t i=0; i<nParams; ++i) {
 | 
|---|
 | 723 |                                                 auto obj = funcDecl->params[i].strict_as<ast::ObjectDecl>();
 | 
|---|
 | 724 |                                                 if (!instantiateArgument(
 | 
|---|
 | 725 |                                                         funcType->params[i], obj->init, args, results, genStart, symtab)) return;
 | 
|---|
 | 726 |                                         }
 | 
|---|
 | 727 |                                         goto endMatch;
 | 
|---|
 | 728 |                                 }
 | 
|---|
 | 729 |                         }
 | 
|---|
 | 730 |                         for ( const auto & param : funcType->params ) {
 | 
|---|
| [2890212] | 731 |                                 // Try adding the arguments corresponding to the current parameter to the existing
 | 
|---|
| [9d5089e] | 732 |                                 // matches
 | 
|---|
| [954c954] | 733 |                                 // no default args for indirect calls
 | 
|---|
| [2890212] | 734 |                                 if ( ! instantiateArgument(
 | 
|---|
| [954c954] | 735 |                                         param, nullptr, args, results, genStart, symtab ) ) return;
 | 
|---|
| [9d5089e] | 736 |                         }
 | 
|---|
 | 737 | 
 | 
|---|
| [954c954] | 738 |                         endMatch:
 | 
|---|
| [9d5089e] | 739 |                         if ( funcType->isVarArgs ) {
 | 
|---|
 | 740 |                                 // append any unused arguments to vararg pack
 | 
|---|
 | 741 |                                 std::size_t genEnd;
 | 
|---|
 | 742 |                                 do {
 | 
|---|
 | 743 |                                         genEnd = results.size();
 | 
|---|
 | 744 | 
 | 
|---|
 | 745 |                                         // iterate results
 | 
|---|
 | 746 |                                         for ( std::size_t i = genStart; i < genEnd; ++i ) {
 | 
|---|
 | 747 |                                                 unsigned nextArg = results[i].nextArg;
 | 
|---|
 | 748 | 
 | 
|---|
 | 749 |                                                 // use remainder of exploded tuple if present
 | 
|---|
 | 750 |                                                 if ( results[i].hasExpl() ) {
 | 
|---|
 | 751 |                                                         const ExplodedArg & expl = results[i].getExpl( args );
 | 
|---|
 | 752 | 
 | 
|---|
 | 753 |                                                         unsigned nextExpl = results[i].nextExpl + 1;
 | 
|---|
 | 754 |                                                         if ( nextExpl == expl.exprs.size() ) { nextExpl = 0; }
 | 
|---|
 | 755 | 
 | 
|---|
 | 756 |                                                         results.emplace_back(
 | 
|---|
 | 757 |                                                                 i, expl.exprs[ results[i].nextExpl ], copy( results[i].env ),
 | 
|---|
 | 758 |                                                                 copy( results[i].need ), copy( results[i].have ),
 | 
|---|
 | 759 |                                                                 copy( results[i].open ), nextArg, 0, Cost::zero, nextExpl,
 | 
|---|
 | 760 |                                                                 results[i].explAlt );
 | 
|---|
 | 761 | 
 | 
|---|
 | 762 |                                                         continue;
 | 
|---|
 | 763 |                                                 }
 | 
|---|
 | 764 | 
 | 
|---|
 | 765 |                                                 // finish result when out of arguments
 | 
|---|
 | 766 |                                                 if ( nextArg >= args.size() ) {
 | 
|---|
 | 767 |                                                         validateFunctionCandidate( func, results[i], results, out );
 | 
|---|
 | 768 | 
 | 
|---|
 | 769 |                                                         continue;
 | 
|---|
 | 770 |                                                 }
 | 
|---|
 | 771 | 
 | 
|---|
 | 772 |                                                 // add each possible next argument
 | 
|---|
 | 773 |                                                 for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) {
 | 
|---|
 | 774 |                                                         const ExplodedArg & expl = args[nextArg][j];
 | 
|---|
 | 775 | 
 | 
|---|
 | 776 |                                                         // fresh copies of parent parameters for this iteration
 | 
|---|
 | 777 |                                                         ast::TypeEnvironment env = results[i].env;
 | 
|---|
 | 778 |                                                         ast::OpenVarSet open = results[i].open;
 | 
|---|
 | 779 | 
 | 
|---|
 | 780 |                                                         env.addActual( expl.env, open );
 | 
|---|
 | 781 | 
 | 
|---|
 | 782 |                                                         // skip empty tuple arguments by (nearly) cloning parent into next gen
 | 
|---|
 | 783 |                                                         if ( expl.exprs.empty() ) {
 | 
|---|
 | 784 |                                                                 results.emplace_back(
 | 
|---|
| [2890212] | 785 |                                                                         results[i], move( env ), copy( results[i].need ),
 | 
|---|
 | 786 |                                                                         copy( results[i].have ), move( open ), nextArg + 1,
 | 
|---|
| [9d5089e] | 787 |                                                                         expl.cost );
 | 
|---|
 | 788 | 
 | 
|---|
 | 789 |                                                                 continue;
 | 
|---|
 | 790 |                                                         }
 | 
|---|
 | 791 | 
 | 
|---|
 | 792 |                                                         // add new result
 | 
|---|
 | 793 |                                                         results.emplace_back(
 | 
|---|
 | 794 |                                                                 i, expl.exprs.front(), move( env ), copy( results[i].need ),
 | 
|---|
| [2890212] | 795 |                                                                 copy( results[i].have ), move( open ), nextArg + 1, 0, expl.cost,
 | 
|---|
| [9d5089e] | 796 |                                                                 expl.exprs.size() == 1 ? 0 : 1, j );
 | 
|---|
 | 797 |                                                 }
 | 
|---|
 | 798 |                                         }
 | 
|---|
 | 799 | 
 | 
|---|
 | 800 |                                         genStart = genEnd;
 | 
|---|
 | 801 |                                 } while( genEnd != results.size() );
 | 
|---|
 | 802 |                         } else {
 | 
|---|
 | 803 |                                 // filter out the results that don't use all the arguments
 | 
|---|
 | 804 |                                 for ( std::size_t i = genStart; i < results.size(); ++i ) {
 | 
|---|
 | 805 |                                         ArgPack & result = results[i];
 | 
|---|
 | 806 |                                         if ( ! result.hasExpl() && result.nextArg >= args.size() ) {
 | 
|---|
 | 807 |                                                 validateFunctionCandidate( func, result, results, out );
 | 
|---|
 | 808 |                                         }
 | 
|---|
 | 809 |                                 }
 | 
|---|
 | 810 |                         }
 | 
|---|
| [4b7cce6] | 811 |                 }
 | 
|---|
 | 812 | 
 | 
|---|
| [432ce7a] | 813 |                 /// Adds implicit struct-conversions to the alternative list
 | 
|---|
 | 814 |                 void addAnonConversions( const CandidateRef & cand ) {
 | 
|---|
| [2890212] | 815 |                         // adds anonymous member interpretations whenever an aggregate value type is seen.
 | 
|---|
 | 816 |                         // it's okay for the aggregate expression to have reference type -- cast it to the
 | 
|---|
| [c8e4d2f8] | 817 |                         // base type to treat the aggregate as the referenced value
 | 
|---|
 | 818 |                         ast::ptr< ast::Expr > aggrExpr( cand->expr );
 | 
|---|
 | 819 |                         ast::ptr< ast::Type > & aggrType = aggrExpr.get_and_mutate()->result;
 | 
|---|
 | 820 |                         cand->env.apply( aggrType );
 | 
|---|
| [2890212] | 821 | 
 | 
|---|
| [c8e4d2f8] | 822 |                         if ( aggrType.as< ast::ReferenceType >() ) {
 | 
|---|
| [b8524ca] | 823 |                                 aggrExpr = new ast::CastExpr{ aggrExpr, aggrType->stripReferences() };
 | 
|---|
| [c8e4d2f8] | 824 |                         }
 | 
|---|
 | 825 | 
 | 
|---|
 | 826 |                         if ( auto structInst = aggrExpr->result.as< ast::StructInstType >() ) {
 | 
|---|
| [898ae07] | 827 |                                 addAggMembers( structInst, aggrExpr, *cand, Cost::safe, "" );
 | 
|---|
| [c8e4d2f8] | 828 |                         } else if ( auto unionInst = aggrExpr->result.as< ast::UnionInstType >() ) {
 | 
|---|
| [898ae07] | 829 |                                 addAggMembers( unionInst, aggrExpr, *cand, Cost::safe, "" );
 | 
|---|
| [c8e4d2f8] | 830 |                         }
 | 
|---|
 | 831 |                 }
 | 
|---|
 | 832 | 
 | 
|---|
 | 833 |                 /// Adds aggregate member interpretations
 | 
|---|
| [2890212] | 834 |                 void addAggMembers(
 | 
|---|
| [98e8b3b] | 835 |                         const ast::BaseInstType * aggrInst, const ast::Expr * expr,
 | 
|---|
| [2890212] | 836 |                         const Candidate & cand, const Cost & addedCost, const std::string & name
 | 
|---|
| [c8e4d2f8] | 837 |                 ) {
 | 
|---|
 | 838 |                         for ( const ast::Decl * decl : aggrInst->lookup( name ) ) {
 | 
|---|
 | 839 |                                 auto dwt = strict_dynamic_cast< const ast::DeclWithType * >( decl );
 | 
|---|
| [2890212] | 840 |                                 CandidateRef newCand = std::make_shared<Candidate>(
 | 
|---|
| [898ae07] | 841 |                                         cand, new ast::MemberExpr{ expr->location, dwt, expr }, addedCost );
 | 
|---|
| [2890212] | 842 |                                 // add anonymous member interpretations whenever an aggregate value type is seen
 | 
|---|
| [c8e4d2f8] | 843 |                                 // as a member expression
 | 
|---|
 | 844 |                                 addAnonConversions( newCand );
 | 
|---|
 | 845 |                                 candidates.emplace_back( move( newCand ) );
 | 
|---|
 | 846 |                         }
 | 
|---|
| [432ce7a] | 847 |                 }
 | 
|---|
 | 848 | 
 | 
|---|
| [898ae07] | 849 |                 /// Adds tuple member interpretations
 | 
|---|
| [2890212] | 850 |                 void addTupleMembers(
 | 
|---|
 | 851 |                         const ast::TupleType * tupleType, const ast::Expr * expr, const Candidate & cand,
 | 
|---|
 | 852 |                         const Cost & addedCost, const ast::Expr * member
 | 
|---|
| [898ae07] | 853 |                 ) {
 | 
|---|
 | 854 |                         if ( auto constantExpr = dynamic_cast< const ast::ConstantExpr * >( member ) ) {
 | 
|---|
| [2890212] | 855 |                                 // get the value of the constant expression as an int, must be between 0 and the
 | 
|---|
| [898ae07] | 856 |                                 // length of the tuple to have meaning
 | 
|---|
 | 857 |                                 long long val = constantExpr->intValue();
 | 
|---|
 | 858 |                                 if ( val >= 0 && (unsigned long long)val < tupleType->size() ) {
 | 
|---|
 | 859 |                                         addCandidate(
 | 
|---|
| [2890212] | 860 |                                                 cand, new ast::TupleIndexExpr{ expr->location, expr, (unsigned)val },
 | 
|---|
| [898ae07] | 861 |                                                 addedCost );
 | 
|---|
 | 862 |                                 }
 | 
|---|
 | 863 |                         }
 | 
|---|
 | 864 |                 }
 | 
|---|
 | 865 | 
 | 
|---|
| [432ce7a] | 866 |                 void postvisit( const ast::UntypedExpr * untypedExpr ) {
 | 
|---|
| [2890212] | 867 |                         std::vector< CandidateFinder > argCandidates =
 | 
|---|
| [432ce7a] | 868 |                                 selfFinder.findSubExprs( untypedExpr->args );
 | 
|---|
| [2890212] | 869 | 
 | 
|---|
| [432ce7a] | 870 |                         // take care of possible tuple assignments
 | 
|---|
 | 871 |                         // if not tuple assignment, handled as normal function call
 | 
|---|
 | 872 |                         Tuples::handleTupleAssignment( selfFinder, untypedExpr, argCandidates );
 | 
|---|
 | 873 | 
 | 
|---|
| [e5c3811] | 874 |                         CandidateFinder funcFinder{ symtab, tenv };
 | 
|---|
 | 875 |                         if (auto nameExpr = untypedExpr->func.as<ast::NameExpr>()) {
 | 
|---|
 | 876 |                                 auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name);
 | 
|---|
 | 877 |                                 if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) {
 | 
|---|
 | 878 |                                         assertf(!argCandidates.empty(), "special function call without argument");
 | 
|---|
 | 879 |                                         for (auto & firstArgCand: argCandidates[0]) {
 | 
|---|
 | 880 |                                                 ast::ptr<ast::Type> argType = firstArgCand->expr->result;
 | 
|---|
 | 881 |                                                 firstArgCand->env.apply(argType);
 | 
|---|
 | 882 |                                                 // strip references
 | 
|---|
 | 883 |                                                 // xxx - is this correct?
 | 
|---|
 | 884 |                                                 while (argType.as<ast::ReferenceType>()) argType = argType.as<ast::ReferenceType>()->base;
 | 
|---|
 | 885 | 
 | 
|---|
 | 886 |                                                 // convert 1-tuple to plain type
 | 
|---|
 | 887 |                                                 if (auto tuple = argType.as<ast::TupleType>()) {
 | 
|---|
 | 888 |                                                         if (tuple->size() == 1) {
 | 
|---|
 | 889 |                                                                 argType = tuple->types[0];
 | 
|---|
 | 890 |                                                         }
 | 
|---|
 | 891 |                                                 }
 | 
|---|
| [943bfad] | 892 | 
 | 
|---|
| [e5c3811] | 893 |                                                 // if argType is an unbound type parameter, all special functions need to be searched.
 | 
|---|
 | 894 |                                                 if (isUnboundType(argType)) {
 | 
|---|
 | 895 |                                                         funcFinder.otypeKeys.clear();
 | 
|---|
 | 896 |                                                         break;
 | 
|---|
 | 897 |                                                 }
 | 
|---|
 | 898 | 
 | 
|---|
 | 899 |                                                 if (argType.as<ast::PointerType>()) funcFinder.otypeKeys.insert(Mangle::Encoding::pointer);
 | 
|---|
 | 900 |                                                 else funcFinder.otypeKeys.insert(Mangle::mangle(argType, Mangle::NoGenericParams | Mangle::Type));
 | 
|---|
 | 901 |                                         }
 | 
|---|
 | 902 |                                 }
 | 
|---|
 | 903 |                         }
 | 
|---|
 | 904 |                         // if candidates are already produced, do not fail
 | 
|---|
 | 905 |                         // xxx - is it possible that handleTupleAssignment and main finder both produce candidates?
 | 
|---|
 | 906 |                         // this means there exists ctor/assign functions with a tuple as first parameter.
 | 
|---|
| [0536c03] | 907 |                         ResolvMode mode = {
 | 
|---|
 | 908 |                                 true, // adjust
 | 
|---|
 | 909 |                                 !untypedExpr->func.as<ast::NameExpr>(), // prune if not calling by name
 | 
|---|
 | 910 |                                 selfFinder.candidates.empty() // failfast if other options are not found
 | 
|---|
 | 911 |                         };
 | 
|---|
 | 912 |                         funcFinder.find( untypedExpr->func, mode );
 | 
|---|
| [e5c3811] | 913 |                         // short-circuit if no candidates
 | 
|---|
 | 914 |                         // if ( funcFinder.candidates.empty() ) return;
 | 
|---|
 | 915 | 
 | 
|---|
 | 916 |                         reason.code = NoMatch;
 | 
|---|
 | 917 | 
 | 
|---|
| [432ce7a] | 918 |                         // find function operators
 | 
|---|
 | 919 |                         ast::ptr< ast::Expr > opExpr = new ast::NameExpr{ untypedExpr->location, "?()" };
 | 
|---|
 | 920 |                         CandidateFinder opFinder{ symtab, tenv };
 | 
|---|
 | 921 |                         // okay if there aren't any function operations
 | 
|---|
 | 922 |                         opFinder.find( opExpr, ResolvMode::withoutFailFast() );
 | 
|---|
 | 923 |                         PRINT(
 | 
|---|
 | 924 |                                 std::cerr << "known function ops:" << std::endl;
 | 
|---|
 | 925 |                                 print( std::cerr, opFinder.candidates, 1 );
 | 
|---|
 | 926 |                         )
 | 
|---|
 | 927 | 
 | 
|---|
 | 928 |                         // pre-explode arguments
 | 
|---|
 | 929 |                         ExplodedArgs_new argExpansions;
 | 
|---|
 | 930 |                         for ( const CandidateFinder & args : argCandidates ) {
 | 
|---|
 | 931 |                                 argExpansions.emplace_back();
 | 
|---|
 | 932 |                                 auto & argE = argExpansions.back();
 | 
|---|
 | 933 |                                 for ( const CandidateRef & arg : args ) { argE.emplace_back( *arg, symtab ); }
 | 
|---|
 | 934 |                         }
 | 
|---|
 | 935 | 
 | 
|---|
 | 936 |                         // Find function matches
 | 
|---|
 | 937 |                         CandidateList found;
 | 
|---|
 | 938 |                         SemanticErrorException errors;
 | 
|---|
 | 939 |                         for ( CandidateRef & func : funcFinder ) {
 | 
|---|
 | 940 |                                 try {
 | 
|---|
 | 941 |                                         PRINT(
 | 
|---|
 | 942 |                                                 std::cerr << "working on alternative:" << std::endl;
 | 
|---|
 | 943 |                                                 print( std::cerr, *func, 2 );
 | 
|---|
 | 944 |                                         )
 | 
|---|
 | 945 | 
 | 
|---|
 | 946 |                                         // check if the type is a pointer to function
 | 
|---|
 | 947 |                                         const ast::Type * funcResult = func->expr->result->stripReferences();
 | 
|---|
 | 948 |                                         if ( auto pointer = dynamic_cast< const ast::PointerType * >( funcResult ) ) {
 | 
|---|
 | 949 |                                                 if ( auto function = pointer->base.as< ast::FunctionType >() ) {
 | 
|---|
 | 950 |                                                         CandidateRef newFunc{ new Candidate{ *func } };
 | 
|---|
| [2890212] | 951 |                                                         newFunc->expr =
 | 
|---|
| [432ce7a] | 952 |                                                                 referenceToRvalueConversion( newFunc->expr, newFunc->cost );
 | 
|---|
 | 953 |                                                         makeFunctionCandidates( newFunc, function, argExpansions, found );
 | 
|---|
 | 954 |                                                 }
 | 
|---|
| [2890212] | 955 |                                         } else if (
 | 
|---|
 | 956 |                                                 auto inst = dynamic_cast< const ast::TypeInstType * >( funcResult )
 | 
|---|
| [432ce7a] | 957 |                                         ) {
 | 
|---|
| [3e5dd913] | 958 |                                                 if ( const ast::EqvClass * clz = func->env.lookup( *inst ) ) {
 | 
|---|
| [432ce7a] | 959 |                                                         if ( auto function = clz->bound.as< ast::FunctionType >() ) {
 | 
|---|
 | 960 |                                                                 CandidateRef newFunc{ new Candidate{ *func } };
 | 
|---|
| [2890212] | 961 |                                                                 newFunc->expr =
 | 
|---|
| [432ce7a] | 962 |                                                                         referenceToRvalueConversion( newFunc->expr, newFunc->cost );
 | 
|---|
 | 963 |                                                                 makeFunctionCandidates( newFunc, function, argExpansions, found );
 | 
|---|
 | 964 |                                                         }
 | 
|---|
 | 965 |                                                 }
 | 
|---|
 | 966 |                                         }
 | 
|---|
 | 967 |                                 } catch ( SemanticErrorException & e ) { errors.append( e ); }
 | 
|---|
 | 968 |                         }
 | 
|---|
 | 969 | 
 | 
|---|
 | 970 |                         // Find matches on function operators `?()`
 | 
|---|
 | 971 |                         if ( ! opFinder.candidates.empty() ) {
 | 
|---|
 | 972 |                                 // add exploded function alternatives to front of argument list
 | 
|---|
 | 973 |                                 std::vector< ExplodedArg > funcE;
 | 
|---|
 | 974 |                                 funcE.reserve( funcFinder.candidates.size() );
 | 
|---|
| [2890212] | 975 |                                 for ( const CandidateRef & func : funcFinder ) {
 | 
|---|
| [432ce7a] | 976 |                                         funcE.emplace_back( *func, symtab );
 | 
|---|
 | 977 |                                 }
 | 
|---|
| [9d5089e] | 978 |                                 argExpansions.emplace_front( move( funcE ) );
 | 
|---|
| [432ce7a] | 979 | 
 | 
|---|
 | 980 |                                 for ( const CandidateRef & op : opFinder ) {
 | 
|---|
 | 981 |                                         try {
 | 
|---|
 | 982 |                                                 // check if type is pointer-to-function
 | 
|---|
 | 983 |                                                 const ast::Type * opResult = op->expr->result->stripReferences();
 | 
|---|
 | 984 |                                                 if ( auto pointer = dynamic_cast< const ast::PointerType * >( opResult ) ) {
 | 
|---|
 | 985 |                                                         if ( auto function = pointer->base.as< ast::FunctionType >() ) {
 | 
|---|
 | 986 |                                                                 CandidateRef newOp{ new Candidate{ *op} };
 | 
|---|
| [2890212] | 987 |                                                                 newOp->expr =
 | 
|---|
| [432ce7a] | 988 |                                                                         referenceToRvalueConversion( newOp->expr, newOp->cost );
 | 
|---|
 | 989 |                                                                 makeFunctionCandidates( newOp, function, argExpansions, found );
 | 
|---|
 | 990 |                                                         }
 | 
|---|
 | 991 |                                                 }
 | 
|---|
 | 992 |                                         } catch ( SemanticErrorException & e ) { errors.append( e ); }
 | 
|---|
 | 993 |                                 }
 | 
|---|
 | 994 |                         }
 | 
|---|
 | 995 | 
 | 
|---|
| [2890212] | 996 |                         // Implement SFINAE; resolution errors are only errors if there aren't any non-error
 | 
|---|
| [432ce7a] | 997 |                         // candidates
 | 
|---|
 | 998 |                         if ( found.empty() && ! errors.isEmpty() ) { throw errors; }
 | 
|---|
 | 999 | 
 | 
|---|
 | 1000 |                         // Compute conversion costs
 | 
|---|
 | 1001 |                         for ( CandidateRef & withFunc : found ) {
 | 
|---|
 | 1002 |                                 Cost cvtCost = computeApplicationConversionCost( withFunc, symtab );
 | 
|---|
 | 1003 | 
 | 
|---|
 | 1004 |                                 PRINT(
 | 
|---|
 | 1005 |                                         auto appExpr = withFunc->expr.strict_as< ast::ApplicationExpr >();
 | 
|---|
 | 1006 |                                         auto pointer = appExpr->func->result.strict_as< ast::PointerType >();
 | 
|---|
 | 1007 |                                         auto function = pointer->base.strict_as< ast::FunctionType >();
 | 
|---|
| [2890212] | 1008 | 
 | 
|---|
| [432ce7a] | 1009 |                                         std::cerr << "Case +++++++++++++ " << appExpr->func << std::endl;
 | 
|---|
 | 1010 |                                         std::cerr << "parameters are:" << std::endl;
 | 
|---|
 | 1011 |                                         ast::printAll( std::cerr, function->params, 2 );
 | 
|---|
 | 1012 |                                         std::cerr << "arguments are:" << std::endl;
 | 
|---|
 | 1013 |                                         ast::printAll( std::cerr, appExpr->args, 2 );
 | 
|---|
 | 1014 |                                         std::cerr << "bindings are:" << std::endl;
 | 
|---|
 | 1015 |                                         ast::print( std::cerr, withFunc->env, 2 );
 | 
|---|
 | 1016 |                                         std::cerr << "cost is: " << withFunc->cost << std::endl;
 | 
|---|
 | 1017 |                                         std::cerr << "cost of conversion is:" << cvtCost << std::endl;
 | 
|---|
 | 1018 |                                 )
 | 
|---|
 | 1019 | 
 | 
|---|
 | 1020 |                                 if ( cvtCost != Cost::infinity ) {
 | 
|---|
 | 1021 |                                         withFunc->cvtCost = cvtCost;
 | 
|---|
| [9d5089e] | 1022 |                                         candidates.emplace_back( move( withFunc ) );
 | 
|---|
| [432ce7a] | 1023 |                                 }
 | 
|---|
 | 1024 |                         }
 | 
|---|
| [9d5089e] | 1025 |                         found = move( candidates );
 | 
|---|
| [432ce7a] | 1026 | 
 | 
|---|
 | 1027 |                         // use a new list so that candidates are not examined by addAnonConversions twice
 | 
|---|
 | 1028 |                         CandidateList winners = findMinCost( found );
 | 
|---|
 | 1029 |                         promoteCvtCost( winners );
 | 
|---|
 | 1030 | 
 | 
|---|
| [2890212] | 1031 |                         // function may return a struct/union value, in which case we need to add candidates
 | 
|---|
 | 1032 |                         // for implicit conversions to each of the anonymous members, which must happen after
 | 
|---|
| [432ce7a] | 1033 |                         // `findMinCost`, since anon conversions are never the cheapest
 | 
|---|
 | 1034 |                         for ( const CandidateRef & c : winners ) {
 | 
|---|
 | 1035 |                                 addAnonConversions( c );
 | 
|---|
 | 1036 |                         }
 | 
|---|
 | 1037 |                         spliceBegin( candidates, winners );
 | 
|---|
 | 1038 | 
 | 
|---|
 | 1039 |                         if ( candidates.empty() && targetType && ! targetType->isVoid() ) {
 | 
|---|
| [2890212] | 1040 |                                 // If resolution is unsuccessful with a target type, try again without, since it
 | 
|---|
| [432ce7a] | 1041 |                                 // will sometimes succeed when it wouldn't with a target type binding.
 | 
|---|
 | 1042 |                                 // For example:
 | 
|---|
 | 1043 |                                 //   forall( otype T ) T & ?[]( T *, ptrdiff_t );
 | 
|---|
 | 1044 |                                 //   const char * x = "hello world";
 | 
|---|
 | 1045 |                                 //   unsigned char ch = x[0];
 | 
|---|
 | 1046 |                                 // Fails with simple return type binding (xxx -- check this!) as follows:
 | 
|---|
 | 1047 |                                 // * T is bound to unsigned char
 | 
|---|
 | 1048 |                                 // * (x: const char *) is unified with unsigned char *, which fails
 | 
|---|
 | 1049 |                                 // xxx -- fix this better
 | 
|---|
 | 1050 |                                 targetType = nullptr;
 | 
|---|
 | 1051 |                                 postvisit( untypedExpr );
 | 
|---|
 | 1052 |                         }
 | 
|---|
 | 1053 |                 }
 | 
|---|
 | 1054 | 
 | 
|---|
| [4b7cce6] | 1055 |                 /// true if expression is an lvalue
 | 
|---|
 | 1056 |                 static bool isLvalue( const ast::Expr * x ) {
 | 
|---|
| [cf32116] | 1057 |                         return x->result && ( x->get_lvalue() || x->result.as< ast::ReferenceType >() );
 | 
|---|
| [4b7cce6] | 1058 |                 }
 | 
|---|
 | 1059 | 
 | 
|---|
 | 1060 |                 void postvisit( const ast::AddressExpr * addressExpr ) {
 | 
|---|
 | 1061 |                         CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1062 |                         finder.find( addressExpr->arg );
 | 
|---|
| [71d6bd8] | 1063 | 
 | 
|---|
 | 1064 |                         if( finder.candidates.empty() ) return;
 | 
|---|
 | 1065 | 
 | 
|---|
 | 1066 |                         reason.code = NoMatch;
 | 
|---|
 | 1067 | 
 | 
|---|
| [4b7cce6] | 1068 |                         for ( CandidateRef & r : finder.candidates ) {
 | 
|---|
 | 1069 |                                 if ( ! isLvalue( r->expr ) ) continue;
 | 
|---|
 | 1070 |                                 addCandidate( *r, new ast::AddressExpr{ addressExpr->location, r->expr } );
 | 
|---|
 | 1071 |                         }
 | 
|---|
 | 1072 |                 }
 | 
|---|
 | 1073 | 
 | 
|---|
 | 1074 |                 void postvisit( const ast::LabelAddressExpr * labelExpr ) {
 | 
|---|
 | 1075 |                         addCandidate( labelExpr, tenv );
 | 
|---|
 | 1076 |                 }
 | 
|---|
 | 1077 | 
 | 
|---|
 | 1078 |                 void postvisit( const ast::CastExpr * castExpr ) {
 | 
|---|
| [c8e4d2f8] | 1079 |                         ast::ptr< ast::Type > toType = castExpr->result;
 | 
|---|
 | 1080 |                         assert( toType );
 | 
|---|
 | 1081 |                         toType = resolveTypeof( toType, symtab );
 | 
|---|
| [3e5dd913] | 1082 |                         // toType = SymTab::validateType( castExpr->location, toType, symtab );
 | 
|---|
| [c8e4d2f8] | 1083 |                         toType = adjustExprType( toType, tenv, symtab );
 | 
|---|
 | 1084 | 
 | 
|---|
 | 1085 |                         CandidateFinder finder{ symtab, tenv, toType };
 | 
|---|
 | 1086 |                         finder.find( castExpr->arg, ResolvMode::withAdjustment() );
 | 
|---|
 | 1087 | 
 | 
|---|
| [71d6bd8] | 1088 |                         if( !finder.candidates.empty() ) reason.code = NoMatch;
 | 
|---|
 | 1089 | 
 | 
|---|
| [c8e4d2f8] | 1090 |                         CandidateList matches;
 | 
|---|
 | 1091 |                         for ( CandidateRef & cand : finder.candidates ) {
 | 
|---|
 | 1092 |                                 ast::AssertionSet need( cand->need.begin(), cand->need.end() ), have;
 | 
|---|
 | 1093 |                                 ast::OpenVarSet open( cand->open );
 | 
|---|
 | 1094 | 
 | 
|---|
 | 1095 |                                 cand->env.extractOpenVars( open );
 | 
|---|
 | 1096 | 
 | 
|---|
| [2890212] | 1097 |                                 // It is possible that a cast can throw away some values in a multiply-valued
 | 
|---|
 | 1098 |                                 // expression, e.g. cast-to-void, one value to zero. Figure out the prefix of the
 | 
|---|
 | 1099 |                                 // subexpression results that are cast directly. The candidate is invalid if it
 | 
|---|
| [c8e4d2f8] | 1100 |                                 // has fewer results than there are types to cast to.
 | 
|---|
 | 1101 |                                 int discardedValues = cand->expr->result->size() - toType->size();
 | 
|---|
 | 1102 |                                 if ( discardedValues < 0 ) continue;
 | 
|---|
 | 1103 | 
 | 
|---|
 | 1104 |                                 // unification run for side-effects
 | 
|---|
 | 1105 |                                 unify( toType, cand->expr->result, cand->env, need, have, open, symtab );
 | 
|---|
| [943bfad] | 1106 |                                 Cost thisCost =
 | 
|---|
| [bb87dd0] | 1107 |                                         (castExpr->isGenerated == ast::GeneratedFlag::GeneratedCast)
 | 
|---|
 | 1108 |                             ? conversionCost( cand->expr->result, toType, cand->expr->get_lvalue(), symtab, cand->env )
 | 
|---|
 | 1109 |                             : castCost( cand->expr->result, toType, cand->expr->get_lvalue(), symtab, cand->env );
 | 
|---|
 | 1110 | 
 | 
|---|
| [c8e4d2f8] | 1111 |                                 PRINT(
 | 
|---|
 | 1112 |                                         std::cerr << "working on cast with result: " << toType << std::endl;
 | 
|---|
 | 1113 |                                         std::cerr << "and expr type: " << cand->expr->result << std::endl;
 | 
|---|
 | 1114 |                                         std::cerr << "env: " << cand->env << std::endl;
 | 
|---|
 | 1115 |                                 )
 | 
|---|
 | 1116 |                                 if ( thisCost != Cost::infinity ) {
 | 
|---|
 | 1117 |                                         PRINT(
 | 
|---|
 | 1118 |                                                 std::cerr << "has finite cost." << std::endl;
 | 
|---|
 | 1119 |                                         )
 | 
|---|
 | 1120 |                                         // count one safe conversion for each value that is thrown away
 | 
|---|
 | 1121 |                                         thisCost.incSafe( discardedValues );
 | 
|---|
| [2890212] | 1122 |                                         CandidateRef newCand = std::make_shared<Candidate>(
 | 
|---|
 | 1123 |                                                 restructureCast( cand->expr, toType, castExpr->isGenerated ),
 | 
|---|
 | 1124 |                                                 copy( cand->env ), move( open ), move( need ), cand->cost,
 | 
|---|
| [c8e4d2f8] | 1125 |                                                 cand->cost + thisCost );
 | 
|---|
 | 1126 |                                         inferParameters( newCand, matches );
 | 
|---|
 | 1127 |                                 }
 | 
|---|
 | 1128 |                         }
 | 
|---|
 | 1129 | 
 | 
|---|
| [898ae07] | 1130 |                         // select first on argument cost, then conversion cost
 | 
|---|
 | 1131 |                         CandidateList minArgCost = findMinCost( matches );
 | 
|---|
 | 1132 |                         promoteCvtCost( minArgCost );
 | 
|---|
 | 1133 |                         candidates = findMinCost( minArgCost );
 | 
|---|
| [4b7cce6] | 1134 |                 }
 | 
|---|
 | 1135 | 
 | 
|---|
 | 1136 |                 void postvisit( const ast::VirtualCastExpr * castExpr ) {
 | 
|---|
 | 1137 |                         assertf( castExpr->result, "Implicit virtual cast targets not yet supported." );
 | 
|---|
 | 1138 |                         CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1139 |                         // don't prune here, all alternatives guaranteed to have same type
 | 
|---|
 | 1140 |                         finder.find( castExpr->arg, ResolvMode::withoutPrune() );
 | 
|---|
 | 1141 |                         for ( CandidateRef & r : finder.candidates ) {
 | 
|---|
| [2890212] | 1142 |                                 addCandidate(
 | 
|---|
 | 1143 |                                         *r,
 | 
|---|
| [c8e4d2f8] | 1144 |                                         new ast::VirtualCastExpr{ castExpr->location, r->expr, castExpr->result } );
 | 
|---|
| [4b7cce6] | 1145 |                         }
 | 
|---|
 | 1146 |                 }
 | 
|---|
 | 1147 | 
 | 
|---|
| [4ef08f7] | 1148 |                 void postvisit( const ast::KeywordCastExpr * castExpr ) {
 | 
|---|
 | 1149 |                         const auto & loc = castExpr->location;
 | 
|---|
 | 1150 |                         assertf( castExpr->result, "Cast target should have been set in Validate." );
 | 
|---|
 | 1151 |                         auto ref = castExpr->result.strict_as<ast::ReferenceType>();
 | 
|---|
 | 1152 |                         auto inst = ref->base.strict_as<ast::StructInstType>();
 | 
|---|
 | 1153 |                         auto target = inst->base.get();
 | 
|---|
 | 1154 | 
 | 
|---|
 | 1155 |                         CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1156 | 
 | 
|---|
 | 1157 |                         auto pick_alternatives = [target, this](CandidateList & found, bool expect_ref) {
 | 
|---|
 | 1158 |                                 for(auto & cand : found) {
 | 
|---|
 | 1159 |                                         const ast::Type * expr = cand->expr->result.get();
 | 
|---|
 | 1160 |                                         if(expect_ref) {
 | 
|---|
 | 1161 |                                                 auto res = dynamic_cast<const ast::ReferenceType*>(expr);
 | 
|---|
 | 1162 |                                                 if(!res) { continue; }
 | 
|---|
 | 1163 |                                                 expr = res->base.get();
 | 
|---|
 | 1164 |                                         }
 | 
|---|
 | 1165 | 
 | 
|---|
 | 1166 |                                         if(auto insttype = dynamic_cast<const ast::TypeInstType*>(expr)) {
 | 
|---|
| [3e5dd913] | 1167 |                                                 auto td = cand->env.lookup(*insttype);
 | 
|---|
| [4ef08f7] | 1168 |                                                 if(!td) { continue; }
 | 
|---|
 | 1169 |                                                 expr = td->bound.get();
 | 
|---|
 | 1170 |                                         }
 | 
|---|
 | 1171 | 
 | 
|---|
 | 1172 |                                         if(auto base = dynamic_cast<const ast::StructInstType*>(expr)) {
 | 
|---|
 | 1173 |                                                 if(base->base == target) {
 | 
|---|
 | 1174 |                                                         candidates.push_back( std::move(cand) );
 | 
|---|
 | 1175 |                                                         reason.code = NoReason;
 | 
|---|
 | 1176 |                                                 }
 | 
|---|
 | 1177 |                                         }
 | 
|---|
 | 1178 |                                 }
 | 
|---|
 | 1179 |                         };
 | 
|---|
 | 1180 | 
 | 
|---|
 | 1181 |                         try {
 | 
|---|
| [7f62b708] | 1182 |                                 // Attempt 1 : turn (thread&)X into (thread$&)X.__thrd
 | 
|---|
| [4ef08f7] | 1183 |                                 // Clone is purely for memory management
 | 
|---|
 | 1184 |                                 std::unique_ptr<const ast::Expr> tech1 { new ast::UntypedMemberExpr(loc, new ast::NameExpr(loc, castExpr->concrete_target.field), castExpr->arg) };
 | 
|---|
 | 1185 | 
 | 
|---|
 | 1186 |                                 // don't prune here, since it's guaranteed all alternatives will have the same type
 | 
|---|
 | 1187 |                                 finder.find( tech1.get(), ResolvMode::withoutPrune() );
 | 
|---|
 | 1188 |                                 pick_alternatives(finder.candidates, false);
 | 
|---|
 | 1189 | 
 | 
|---|
 | 1190 |                                 return;
 | 
|---|
 | 1191 |                         } catch(SemanticErrorException & ) {}
 | 
|---|
 | 1192 | 
 | 
|---|
| [7f62b708] | 1193 |                         // Fallback : turn (thread&)X into (thread$&)get_thread(X)
 | 
|---|
| [4ef08f7] | 1194 |                         std::unique_ptr<const ast::Expr> fallback { ast::UntypedExpr::createDeref(loc,  new ast::UntypedExpr(loc, new ast::NameExpr(loc, castExpr->concrete_target.getter), { castExpr->arg })) };
 | 
|---|
 | 1195 |                         // don't prune here, since it's guaranteed all alternatives will have the same type
 | 
|---|
 | 1196 |                         finder.find( fallback.get(), ResolvMode::withoutPrune() );
 | 
|---|
 | 1197 | 
 | 
|---|
 | 1198 |                         pick_alternatives(finder.candidates, true);
 | 
|---|
 | 1199 | 
 | 
|---|
 | 1200 |                         // Whatever happens here, we have no more fallbacks
 | 
|---|
 | 1201 |                 }
 | 
|---|
 | 1202 | 
 | 
|---|
| [4b7cce6] | 1203 |                 void postvisit( const ast::UntypedMemberExpr * memberExpr ) {
 | 
|---|
| [898ae07] | 1204 |                         CandidateFinder aggFinder{ symtab, tenv };
 | 
|---|
 | 1205 |                         aggFinder.find( memberExpr->aggregate, ResolvMode::withAdjustment() );
 | 
|---|
 | 1206 |                         for ( CandidateRef & agg : aggFinder.candidates ) {
 | 
|---|
| [2890212] | 1207 |                                 // it's okay for the aggregate expression to have reference type -- cast it to the
 | 
|---|
| [898ae07] | 1208 |                                 // base type to treat the aggregate as the referenced value
 | 
|---|
 | 1209 |                                 Cost addedCost = Cost::zero;
 | 
|---|
 | 1210 |                                 agg->expr = referenceToRvalueConversion( agg->expr, addedCost );
 | 
|---|
 | 1211 | 
 | 
|---|
 | 1212 |                                 // find member of the given type
 | 
|---|
 | 1213 |                                 if ( auto structInst = agg->expr->result.as< ast::StructInstType >() ) {
 | 
|---|
| [2890212] | 1214 |                                         addAggMembers(
 | 
|---|
| [898ae07] | 1215 |                                                 structInst, agg->expr, *agg, addedCost, getMemberName( memberExpr ) );
 | 
|---|
 | 1216 |                                 } else if ( auto unionInst = agg->expr->result.as< ast::UnionInstType >() ) {
 | 
|---|
| [2890212] | 1217 |                                         addAggMembers(
 | 
|---|
| [898ae07] | 1218 |                                                 unionInst, agg->expr, *agg, addedCost, getMemberName( memberExpr ) );
 | 
|---|
 | 1219 |                                 } else if ( auto tupleType = agg->expr->result.as< ast::TupleType >() ) {
 | 
|---|
 | 1220 |                                         addTupleMembers( tupleType, agg->expr, *agg, addedCost, memberExpr->member );
 | 
|---|
 | 1221 |                                 }
 | 
|---|
 | 1222 |                         }
 | 
|---|
| [4b7cce6] | 1223 |                 }
 | 
|---|
 | 1224 | 
 | 
|---|
 | 1225 |                 void postvisit( const ast::MemberExpr * memberExpr ) {
 | 
|---|
 | 1226 |                         addCandidate( memberExpr, tenv );
 | 
|---|
 | 1227 |                 }
 | 
|---|
 | 1228 | 
 | 
|---|
| [898ae07] | 1229 |                 void postvisit( const ast::NameExpr * nameExpr ) {
 | 
|---|
| [e5c3811] | 1230 |                         std::vector< ast::SymbolTable::IdData > declList;
 | 
|---|
 | 1231 |                         if (!selfFinder.otypeKeys.empty()) {
 | 
|---|
 | 1232 |                                 auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name);
 | 
|---|
 | 1233 |                                 assertf(kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS, "special lookup with non-special target: %s", nameExpr->name.c_str());
 | 
|---|
 | 1234 | 
 | 
|---|
 | 1235 |                                 for (auto & otypeKey: selfFinder.otypeKeys) {
 | 
|---|
 | 1236 |                                         auto result = symtab.specialLookupId(kind, otypeKey);
 | 
|---|
 | 1237 |                                         declList.insert(declList.end(), std::make_move_iterator(result.begin()), std::make_move_iterator(result.end()));
 | 
|---|
 | 1238 |                                 }
 | 
|---|
 | 1239 |                         }
 | 
|---|
 | 1240 |                         else {
 | 
|---|
 | 1241 |                                 declList = symtab.lookupId( nameExpr->name );
 | 
|---|
 | 1242 |                         }
 | 
|---|
| [898ae07] | 1243 |                         PRINT( std::cerr << "nameExpr is " << nameExpr->name << std::endl; )
 | 
|---|
| [e5c3811] | 1244 | 
 | 
|---|
| [71d6bd8] | 1245 |                         if( declList.empty() ) return;
 | 
|---|
 | 1246 | 
 | 
|---|
 | 1247 |                         reason.code = NoMatch;
 | 
|---|
 | 1248 | 
 | 
|---|
| [898ae07] | 1249 |                         for ( auto & data : declList ) {
 | 
|---|
 | 1250 |                                 Cost cost = Cost::zero;
 | 
|---|
 | 1251 |                                 ast::Expr * newExpr = data.combine( nameExpr->location, cost );
 | 
|---|
 | 1252 | 
 | 
|---|
 | 1253 |                                 CandidateRef newCand = std::make_shared<Candidate>(
 | 
|---|
| [2890212] | 1254 |                                         newExpr, copy( tenv ), ast::OpenVarSet{}, ast::AssertionSet{}, Cost::zero,
 | 
|---|
| [898ae07] | 1255 |                                         cost );
 | 
|---|
 | 1256 |                                 PRINT(
 | 
|---|
 | 1257 |                                         std::cerr << "decl is ";
 | 
|---|
 | 1258 |                                         ast::print( std::cerr, data.id );
 | 
|---|
 | 1259 |                                         std::cerr << std::endl;
 | 
|---|
 | 1260 |                                         std::cerr << "newExpr is ";
 | 
|---|
 | 1261 |                                         ast::print( std::cerr, newExpr );
 | 
|---|
 | 1262 |                                         std::cerr << std::endl;
 | 
|---|
 | 1263 |                                 )
 | 
|---|
| [2890212] | 1264 |                                 newCand->expr = ast::mutate_field(
 | 
|---|
 | 1265 |                                         newCand->expr.get(), &ast::Expr::result,
 | 
|---|
| [898ae07] | 1266 |                                         renameTyVars( newCand->expr->result ) );
 | 
|---|
| [2890212] | 1267 |                                 // add anonymous member interpretations whenever an aggregate value type is seen
 | 
|---|
| [898ae07] | 1268 |                                 // as a name expression
 | 
|---|
 | 1269 |                                 addAnonConversions( newCand );
 | 
|---|
 | 1270 |                                 candidates.emplace_back( move( newCand ) );
 | 
|---|
 | 1271 |                         }
 | 
|---|
| [4b7cce6] | 1272 |                 }
 | 
|---|
 | 1273 | 
 | 
|---|
 | 1274 |                 void postvisit( const ast::VariableExpr * variableExpr ) {
 | 
|---|
 | 1275 |                         // not sufficient to just pass `variableExpr` here, type might have changed since
 | 
|---|
 | 1276 |                         // creation
 | 
|---|
| [2890212] | 1277 |                         addCandidate(
 | 
|---|
| [4b7cce6] | 1278 |                                 new ast::VariableExpr{ variableExpr->location, variableExpr->var }, tenv );
 | 
|---|
 | 1279 |                 }
 | 
|---|
 | 1280 | 
 | 
|---|
 | 1281 |                 void postvisit( const ast::ConstantExpr * constantExpr ) {
 | 
|---|
 | 1282 |                         addCandidate( constantExpr, tenv );
 | 
|---|
 | 1283 |                 }
 | 
|---|
 | 1284 | 
 | 
|---|
 | 1285 |                 void postvisit( const ast::SizeofExpr * sizeofExpr ) {
 | 
|---|
| [898ae07] | 1286 |                         if ( sizeofExpr->type ) {
 | 
|---|
| [2890212] | 1287 |                                 addCandidate(
 | 
|---|
 | 1288 |                                         new ast::SizeofExpr{
 | 
|---|
 | 1289 |                                                 sizeofExpr->location, resolveTypeof( sizeofExpr->type, symtab ) },
 | 
|---|
| [898ae07] | 1290 |                                         tenv );
 | 
|---|
 | 1291 |                         } else {
 | 
|---|
 | 1292 |                                 // find all candidates for the argument to sizeof
 | 
|---|
 | 1293 |                                 CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1294 |                                 finder.find( sizeofExpr->expr );
 | 
|---|
 | 1295 |                                 // find the lowest-cost candidate, otherwise ambiguous
 | 
|---|
 | 1296 |                                 CandidateList winners = findMinCost( finder.candidates );
 | 
|---|
 | 1297 |                                 if ( winners.size() != 1 ) {
 | 
|---|
| [2890212] | 1298 |                                         SemanticError(
 | 
|---|
| [898ae07] | 1299 |                                                 sizeofExpr->expr.get(), "Ambiguous expression in sizeof operand: " );
 | 
|---|
 | 1300 |                                 }
 | 
|---|
 | 1301 |                                 // return the lowest-cost candidate
 | 
|---|
 | 1302 |                                 CandidateRef & choice = winners.front();
 | 
|---|
 | 1303 |                                 choice->expr = referenceToRvalueConversion( choice->expr, choice->cost );
 | 
|---|
 | 1304 |                                 choice->cost = Cost::zero;
 | 
|---|
 | 1305 |                                 addCandidate( *choice, new ast::SizeofExpr{ sizeofExpr->location, choice->expr } );
 | 
|---|
 | 1306 |                         }
 | 
|---|
| [4b7cce6] | 1307 |                 }
 | 
|---|
 | 1308 | 
 | 
|---|
 | 1309 |                 void postvisit( const ast::AlignofExpr * alignofExpr ) {
 | 
|---|
| [898ae07] | 1310 |                         if ( alignofExpr->type ) {
 | 
|---|
| [2890212] | 1311 |                                 addCandidate(
 | 
|---|
 | 1312 |                                         new ast::AlignofExpr{
 | 
|---|
 | 1313 |                                                 alignofExpr->location, resolveTypeof( alignofExpr->type, symtab ) },
 | 
|---|
| [898ae07] | 1314 |                                         tenv );
 | 
|---|
 | 1315 |                         } else {
 | 
|---|
 | 1316 |                                 // find all candidates for the argument to alignof
 | 
|---|
 | 1317 |                                 CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1318 |                                 finder.find( alignofExpr->expr );
 | 
|---|
 | 1319 |                                 // find the lowest-cost candidate, otherwise ambiguous
 | 
|---|
 | 1320 |                                 CandidateList winners = findMinCost( finder.candidates );
 | 
|---|
 | 1321 |                                 if ( winners.size() != 1 ) {
 | 
|---|
| [2890212] | 1322 |                                         SemanticError(
 | 
|---|
| [898ae07] | 1323 |                                                 alignofExpr->expr.get(), "Ambiguous expression in alignof operand: " );
 | 
|---|
 | 1324 |                                 }
 | 
|---|
 | 1325 |                                 // return the lowest-cost candidate
 | 
|---|
 | 1326 |                                 CandidateRef & choice = winners.front();
 | 
|---|
 | 1327 |                                 choice->expr = referenceToRvalueConversion( choice->expr, choice->cost );
 | 
|---|
 | 1328 |                                 choice->cost = Cost::zero;
 | 
|---|
| [2890212] | 1329 |                                 addCandidate(
 | 
|---|
| [898ae07] | 1330 |                                         *choice, new ast::AlignofExpr{ alignofExpr->location, choice->expr } );
 | 
|---|
 | 1331 |                         }
 | 
|---|
| [4b7cce6] | 1332 |                 }
 | 
|---|
 | 1333 | 
 | 
|---|
 | 1334 |                 void postvisit( const ast::UntypedOffsetofExpr * offsetofExpr ) {
 | 
|---|
| [98e8b3b] | 1335 |                         const ast::BaseInstType * aggInst;
 | 
|---|
| [898ae07] | 1336 |                         if (( aggInst = offsetofExpr->type.as< ast::StructInstType >() )) ;
 | 
|---|
 | 1337 |                         else if (( aggInst = offsetofExpr->type.as< ast::UnionInstType >() )) ;
 | 
|---|
 | 1338 |                         else return;
 | 
|---|
 | 1339 | 
 | 
|---|
 | 1340 |                         for ( const ast::Decl * member : aggInst->lookup( offsetofExpr->member ) ) {
 | 
|---|
 | 1341 |                                 auto dwt = strict_dynamic_cast< const ast::DeclWithType * >( member );
 | 
|---|
| [2890212] | 1342 |                                 addCandidate(
 | 
|---|
| [898ae07] | 1343 |                                         new ast::OffsetofExpr{ offsetofExpr->location, aggInst, dwt }, tenv );
 | 
|---|
 | 1344 |                         }
 | 
|---|
| [4b7cce6] | 1345 |                 }
 | 
|---|
 | 1346 | 
 | 
|---|
 | 1347 |                 void postvisit( const ast::OffsetofExpr * offsetofExpr ) {
 | 
|---|
 | 1348 |                         addCandidate( offsetofExpr, tenv );
 | 
|---|
 | 1349 |                 }
 | 
|---|
 | 1350 | 
 | 
|---|
 | 1351 |                 void postvisit( const ast::OffsetPackExpr * offsetPackExpr ) {
 | 
|---|
 | 1352 |                         addCandidate( offsetPackExpr, tenv );
 | 
|---|
 | 1353 |                 }
 | 
|---|
 | 1354 | 
 | 
|---|
 | 1355 |                 void postvisit( const ast::LogicalExpr * logicalExpr ) {
 | 
|---|
 | 1356 |                         CandidateFinder finder1{ symtab, tenv };
 | 
|---|
 | 1357 |                         finder1.find( logicalExpr->arg1, ResolvMode::withAdjustment() );
 | 
|---|
 | 1358 |                         if ( finder1.candidates.empty() ) return;
 | 
|---|
 | 1359 | 
 | 
|---|
 | 1360 |                         CandidateFinder finder2{ symtab, tenv };
 | 
|---|
 | 1361 |                         finder2.find( logicalExpr->arg2, ResolvMode::withAdjustment() );
 | 
|---|
 | 1362 |                         if ( finder2.candidates.empty() ) return;
 | 
|---|
 | 1363 | 
 | 
|---|
| [71d6bd8] | 1364 |                         reason.code = NoMatch;
 | 
|---|
 | 1365 | 
 | 
|---|
| [4b7cce6] | 1366 |                         for ( const CandidateRef & r1 : finder1.candidates ) {
 | 
|---|
 | 1367 |                                 for ( const CandidateRef & r2 : finder2.candidates ) {
 | 
|---|
 | 1368 |                                         ast::TypeEnvironment env{ r1->env };
 | 
|---|
 | 1369 |                                         env.simpleCombine( r2->env );
 | 
|---|
 | 1370 |                                         ast::OpenVarSet open{ r1->open };
 | 
|---|
 | 1371 |                                         mergeOpenVars( open, r2->open );
 | 
|---|
 | 1372 |                                         ast::AssertionSet need;
 | 
|---|
 | 1373 |                                         mergeAssertionSet( need, r1->need );
 | 
|---|
 | 1374 |                                         mergeAssertionSet( need, r2->need );
 | 
|---|
 | 1375 | 
 | 
|---|
 | 1376 |                                         addCandidate(
 | 
|---|
| [2890212] | 1377 |                                                 new ast::LogicalExpr{
 | 
|---|
| [4b7cce6] | 1378 |                                                         logicalExpr->location, r1->expr, r2->expr, logicalExpr->isAnd },
 | 
|---|
| [9d5089e] | 1379 |                                                 move( env ), move( open ), move( need ), r1->cost + r2->cost );
 | 
|---|
| [4b7cce6] | 1380 |                                 }
 | 
|---|
 | 1381 |                         }
 | 
|---|
 | 1382 |                 }
 | 
|---|
 | 1383 | 
 | 
|---|
 | 1384 |                 void postvisit( const ast::ConditionalExpr * conditionalExpr ) {
 | 
|---|
 | 1385 |                         // candidates for condition
 | 
|---|
 | 1386 |                         CandidateFinder finder1{ symtab, tenv };
 | 
|---|
 | 1387 |                         finder1.find( conditionalExpr->arg1, ResolvMode::withAdjustment() );
 | 
|---|
 | 1388 |                         if ( finder1.candidates.empty() ) return;
 | 
|---|
 | 1389 | 
 | 
|---|
 | 1390 |                         // candidates for true result
 | 
|---|
 | 1391 |                         CandidateFinder finder2{ symtab, tenv };
 | 
|---|
 | 1392 |                         finder2.find( conditionalExpr->arg2, ResolvMode::withAdjustment() );
 | 
|---|
 | 1393 |                         if ( finder2.candidates.empty() ) return;
 | 
|---|
 | 1394 | 
 | 
|---|
 | 1395 |                         // candidates for false result
 | 
|---|
 | 1396 |                         CandidateFinder finder3{ symtab, tenv };
 | 
|---|
 | 1397 |                         finder3.find( conditionalExpr->arg3, ResolvMode::withAdjustment() );
 | 
|---|
 | 1398 |                         if ( finder3.candidates.empty() ) return;
 | 
|---|
 | 1399 | 
 | 
|---|
| [71d6bd8] | 1400 |                         reason.code = NoMatch;
 | 
|---|
 | 1401 | 
 | 
|---|
| [4b7cce6] | 1402 |                         for ( const CandidateRef & r1 : finder1.candidates ) {
 | 
|---|
 | 1403 |                                 for ( const CandidateRef & r2 : finder2.candidates ) {
 | 
|---|
 | 1404 |                                         for ( const CandidateRef & r3 : finder3.candidates ) {
 | 
|---|
 | 1405 |                                                 ast::TypeEnvironment env{ r1->env };
 | 
|---|
 | 1406 |                                                 env.simpleCombine( r2->env );
 | 
|---|
 | 1407 |                                                 env.simpleCombine( r3->env );
 | 
|---|
 | 1408 |                                                 ast::OpenVarSet open{ r1->open };
 | 
|---|
 | 1409 |                                                 mergeOpenVars( open, r2->open );
 | 
|---|
 | 1410 |                                                 mergeOpenVars( open, r3->open );
 | 
|---|
 | 1411 |                                                 ast::AssertionSet need;
 | 
|---|
 | 1412 |                                                 mergeAssertionSet( need, r1->need );
 | 
|---|
 | 1413 |                                                 mergeAssertionSet( need, r2->need );
 | 
|---|
 | 1414 |                                                 mergeAssertionSet( need, r3->need );
 | 
|---|
 | 1415 |                                                 ast::AssertionSet have;
 | 
|---|
 | 1416 | 
 | 
|---|
| [2890212] | 1417 |                                                 // unify true and false results, then infer parameters to produce new
 | 
|---|
| [4b7cce6] | 1418 |                                                 // candidates
 | 
|---|
 | 1419 |                                                 ast::ptr< ast::Type > common;
 | 
|---|
| [2890212] | 1420 |                                                 if (
 | 
|---|
 | 1421 |                                                         unify(
 | 
|---|
 | 1422 |                                                                 r2->expr->result, r3->expr->result, env, need, have, open, symtab,
 | 
|---|
 | 1423 |                                                                 common )
 | 
|---|
| [4b7cce6] | 1424 |                                                 ) {
 | 
|---|
| [898ae07] | 1425 |                                                         // generate typed expression
 | 
|---|
| [2890212] | 1426 |                                                         ast::ConditionalExpr * newExpr = new ast::ConditionalExpr{
 | 
|---|
| [898ae07] | 1427 |                                                                 conditionalExpr->location, r1->expr, r2->expr, r3->expr };
 | 
|---|
 | 1428 |                                                         newExpr->result = common ? common : r2->expr->result;
 | 
|---|
 | 1429 |                                                         // convert both options to result type
 | 
|---|
 | 1430 |                                                         Cost cost = r1->cost + r2->cost + r3->cost;
 | 
|---|
| [2890212] | 1431 |                                                         newExpr->arg2 = computeExpressionConversionCost(
 | 
|---|
| [898ae07] | 1432 |                                                                 newExpr->arg2, newExpr->result, symtab, env, cost );
 | 
|---|
 | 1433 |                                                         newExpr->arg3 = computeExpressionConversionCost(
 | 
|---|
 | 1434 |                                                                 newExpr->arg3, newExpr->result, symtab, env, cost );
 | 
|---|
 | 1435 |                                                         // output candidate
 | 
|---|
 | 1436 |                                                         CandidateRef newCand = std::make_shared<Candidate>(
 | 
|---|
 | 1437 |                                                                 newExpr, move( env ), move( open ), move( need ), cost );
 | 
|---|
 | 1438 |                                                         inferParameters( newCand, candidates );
 | 
|---|
| [4b7cce6] | 1439 |                                                 }
 | 
|---|
 | 1440 |                                         }
 | 
|---|
 | 1441 |                                 }
 | 
|---|
 | 1442 |                         }
 | 
|---|
 | 1443 |                 }
 | 
|---|
 | 1444 | 
 | 
|---|
 | 1445 |                 void postvisit( const ast::CommaExpr * commaExpr ) {
 | 
|---|
 | 1446 |                         ast::TypeEnvironment env{ tenv };
 | 
|---|
 | 1447 |                         ast::ptr< ast::Expr > arg1 = resolveInVoidContext( commaExpr->arg1, symtab, env );
 | 
|---|
| [2890212] | 1448 | 
 | 
|---|
| [4b7cce6] | 1449 |                         CandidateFinder finder2{ symtab, env };
 | 
|---|
 | 1450 |                         finder2.find( commaExpr->arg2, ResolvMode::withAdjustment() );
 | 
|---|
 | 1451 | 
 | 
|---|
 | 1452 |                         for ( const CandidateRef & r2 : finder2.candidates ) {
 | 
|---|
 | 1453 |                                 addCandidate( *r2, new ast::CommaExpr{ commaExpr->location, arg1, r2->expr } );
 | 
|---|
 | 1454 |                         }
 | 
|---|
 | 1455 |                 }
 | 
|---|
 | 1456 | 
 | 
|---|
 | 1457 |                 void postvisit( const ast::ImplicitCopyCtorExpr * ctorExpr ) {
 | 
|---|
 | 1458 |                         addCandidate( ctorExpr, tenv );
 | 
|---|
 | 1459 |                 }
 | 
|---|
 | 1460 | 
 | 
|---|
 | 1461 |                 void postvisit( const ast::ConstructorExpr * ctorExpr ) {
 | 
|---|
 | 1462 |                         CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1463 |                         finder.find( ctorExpr->callExpr, ResolvMode::withoutPrune() );
 | 
|---|
 | 1464 |                         for ( CandidateRef & r : finder.candidates ) {
 | 
|---|
 | 1465 |                                 addCandidate( *r, new ast::ConstructorExpr{ ctorExpr->location, r->expr } );
 | 
|---|
 | 1466 |                         }
 | 
|---|
 | 1467 |                 }
 | 
|---|
 | 1468 | 
 | 
|---|
 | 1469 |                 void postvisit( const ast::RangeExpr * rangeExpr ) {
 | 
|---|
 | 1470 |                         // resolve low and high, accept candidates where low and high types unify
 | 
|---|
 | 1471 |                         CandidateFinder finder1{ symtab, tenv };
 | 
|---|
 | 1472 |                         finder1.find( rangeExpr->low, ResolvMode::withAdjustment() );
 | 
|---|
 | 1473 |                         if ( finder1.candidates.empty() ) return;
 | 
|---|
 | 1474 | 
 | 
|---|
 | 1475 |                         CandidateFinder finder2{ symtab, tenv };
 | 
|---|
 | 1476 |                         finder2.find( rangeExpr->high, ResolvMode::withAdjustment() );
 | 
|---|
 | 1477 |                         if ( finder2.candidates.empty() ) return;
 | 
|---|
 | 1478 | 
 | 
|---|
| [71d6bd8] | 1479 |                         reason.code = NoMatch;
 | 
|---|
 | 1480 | 
 | 
|---|
| [4b7cce6] | 1481 |                         for ( const CandidateRef & r1 : finder1.candidates ) {
 | 
|---|
 | 1482 |                                 for ( const CandidateRef & r2 : finder2.candidates ) {
 | 
|---|
 | 1483 |                                         ast::TypeEnvironment env{ r1->env };
 | 
|---|
 | 1484 |                                         env.simpleCombine( r2->env );
 | 
|---|
 | 1485 |                                         ast::OpenVarSet open{ r1->open };
 | 
|---|
 | 1486 |                                         mergeOpenVars( open, r2->open );
 | 
|---|
 | 1487 |                                         ast::AssertionSet need;
 | 
|---|
 | 1488 |                                         mergeAssertionSet( need, r1->need );
 | 
|---|
 | 1489 |                                         mergeAssertionSet( need, r2->need );
 | 
|---|
 | 1490 |                                         ast::AssertionSet have;
 | 
|---|
 | 1491 | 
 | 
|---|
 | 1492 |                                         ast::ptr< ast::Type > common;
 | 
|---|
| [2890212] | 1493 |                                         if (
 | 
|---|
 | 1494 |                                                 unify(
 | 
|---|
 | 1495 |                                                         r1->expr->result, r2->expr->result, env, need, have, open, symtab,
 | 
|---|
 | 1496 |                                                         common )
 | 
|---|
| [4b7cce6] | 1497 |                                         ) {
 | 
|---|
| [898ae07] | 1498 |                                                 // generate new expression
 | 
|---|
| [2890212] | 1499 |                                                 ast::RangeExpr * newExpr =
 | 
|---|
| [4b7cce6] | 1500 |                                                         new ast::RangeExpr{ rangeExpr->location, r1->expr, r2->expr };
 | 
|---|
 | 1501 |                                                 newExpr->result = common ? common : r1->expr->result;
 | 
|---|
| [898ae07] | 1502 |                                                 // add candidate
 | 
|---|
 | 1503 |                                                 CandidateRef newCand = std::make_shared<Candidate>(
 | 
|---|
| [2890212] | 1504 |                                                         newExpr, move( env ), move( open ), move( need ),
 | 
|---|
| [898ae07] | 1505 |                                                         r1->cost + r2->cost );
 | 
|---|
 | 1506 |                                                 inferParameters( newCand, candidates );
 | 
|---|
| [4b7cce6] | 1507 |                                         }
 | 
|---|
 | 1508 |                                 }
 | 
|---|
 | 1509 |                         }
 | 
|---|
 | 1510 |                 }
 | 
|---|
 | 1511 | 
 | 
|---|
 | 1512 |                 void postvisit( const ast::UntypedTupleExpr * tupleExpr ) {
 | 
|---|
| [2890212] | 1513 |                         std::vector< CandidateFinder > subCandidates =
 | 
|---|
| [4b7cce6] | 1514 |                                 selfFinder.findSubExprs( tupleExpr->exprs );
 | 
|---|
 | 1515 |                         std::vector< CandidateList > possibilities;
 | 
|---|
 | 1516 |                         combos( subCandidates.begin(), subCandidates.end(), back_inserter( possibilities ) );
 | 
|---|
 | 1517 | 
 | 
|---|
 | 1518 |                         for ( const CandidateList & subs : possibilities ) {
 | 
|---|
 | 1519 |                                 std::vector< ast::ptr< ast::Expr > > exprs;
 | 
|---|
 | 1520 |                                 exprs.reserve( subs.size() );
 | 
|---|
 | 1521 |                                 for ( const CandidateRef & sub : subs ) { exprs.emplace_back( sub->expr ); }
 | 
|---|
 | 1522 | 
 | 
|---|
 | 1523 |                                 ast::TypeEnvironment env;
 | 
|---|
 | 1524 |                                 ast::OpenVarSet open;
 | 
|---|
 | 1525 |                                 ast::AssertionSet need;
 | 
|---|
 | 1526 |                                 for ( const CandidateRef & sub : subs ) {
 | 
|---|
 | 1527 |                                         env.simpleCombine( sub->env );
 | 
|---|
 | 1528 |                                         mergeOpenVars( open, sub->open );
 | 
|---|
 | 1529 |                                         mergeAssertionSet( need, sub->need );
 | 
|---|
 | 1530 |                                 }
 | 
|---|
 | 1531 | 
 | 
|---|
 | 1532 |                                 addCandidate(
 | 
|---|
| [2890212] | 1533 |                                         new ast::TupleExpr{ tupleExpr->location, move( exprs ) },
 | 
|---|
| [9d5089e] | 1534 |                                         move( env ), move( open ), move( need ), sumCost( subs ) );
 | 
|---|
| [4b7cce6] | 1535 |                         }
 | 
|---|
 | 1536 |                 }
 | 
|---|
 | 1537 | 
 | 
|---|
 | 1538 |                 void postvisit( const ast::TupleExpr * tupleExpr ) {
 | 
|---|
 | 1539 |                         addCandidate( tupleExpr, tenv );
 | 
|---|
 | 1540 |                 }
 | 
|---|
 | 1541 | 
 | 
|---|
 | 1542 |                 void postvisit( const ast::TupleIndexExpr * tupleExpr ) {
 | 
|---|
 | 1543 |                         addCandidate( tupleExpr, tenv );
 | 
|---|
 | 1544 |                 }
 | 
|---|
 | 1545 | 
 | 
|---|
 | 1546 |                 void postvisit( const ast::TupleAssignExpr * tupleExpr ) {
 | 
|---|
 | 1547 |                         addCandidate( tupleExpr, tenv );
 | 
|---|
 | 1548 |                 }
 | 
|---|
 | 1549 | 
 | 
|---|
 | 1550 |                 void postvisit( const ast::UniqueExpr * unqExpr ) {
 | 
|---|
 | 1551 |                         CandidateFinder finder{ symtab, tenv };
 | 
|---|
 | 1552 |                         finder.find( unqExpr->expr, ResolvMode::withAdjustment() );
 | 
|---|
 | 1553 |                         for ( CandidateRef & r : finder.candidates ) {
 | 
|---|
 | 1554 |                                 // ensure that the the id is passed on so that the expressions are "linked"
 | 
|---|
 | 1555 |                                 addCandidate( *r, new ast::UniqueExpr{ unqExpr->location, r->expr, unqExpr->id } );
 | 
|---|
 | 1556 |                         }
 | 
|---|
 | 1557 |                 }
 | 
|---|
 | 1558 | 
 | 
|---|
 | 1559 |                 void postvisit( const ast::StmtExpr * stmtExpr ) {
 | 
|---|
| [17a0ede2] | 1560 |                         addCandidate( resolveStmtExpr( stmtExpr, symtab ), tenv );
 | 
|---|
| [4b7cce6] | 1561 |                 }
 | 
|---|
 | 1562 | 
 | 
|---|
 | 1563 |                 void postvisit( const ast::UntypedInitExpr * initExpr ) {
 | 
|---|
| [17a0ede2] | 1564 |                         // handle each option like a cast
 | 
|---|
 | 1565 |                         CandidateList matches;
 | 
|---|
 | 1566 |                         PRINT(
 | 
|---|
 | 1567 |                                 std::cerr << "untyped init expr: " << initExpr << std::endl;
 | 
|---|
 | 1568 |                         )
 | 
|---|
 | 1569 |                         // O(n^2) checks of d-types with e-types
 | 
|---|
 | 1570 |                         for ( const ast::InitAlternative & initAlt : initExpr->initAlts ) {
 | 
|---|
 | 1571 |                                 // calculate target type
 | 
|---|
 | 1572 |                                 const ast::Type * toType = resolveTypeof( initAlt.type, symtab );
 | 
|---|
| [3e5dd913] | 1573 |                                 // toType = SymTab::validateType( initExpr->location, toType, symtab );
 | 
|---|
| [17a0ede2] | 1574 |                                 toType = adjustExprType( toType, tenv, symtab );
 | 
|---|
| [2890212] | 1575 |                                 // The call to find must occur inside this loop, otherwise polymorphic return
 | 
|---|
 | 1576 |                                 // types are not bound to the initialization type, since return type variables are
 | 
|---|
 | 1577 |                                 // only open for the duration of resolving the UntypedExpr.
 | 
|---|
| [17a0ede2] | 1578 |                                 CandidateFinder finder{ symtab, tenv, toType };
 | 
|---|
 | 1579 |                                 finder.find( initExpr->expr, ResolvMode::withAdjustment() );
 | 
|---|
 | 1580 |                                 for ( CandidateRef & cand : finder.candidates ) {
 | 
|---|
| [71d6bd8] | 1581 |                                         if(reason.code == NotFound) reason.code = NoMatch;
 | 
|---|
 | 1582 | 
 | 
|---|
| [17a0ede2] | 1583 |                                         ast::TypeEnvironment env{ cand->env };
 | 
|---|
 | 1584 |                                         ast::AssertionSet need( cand->need.begin(), cand->need.end() ), have;
 | 
|---|
 | 1585 |                                         ast::OpenVarSet open{ cand->open };
 | 
|---|
 | 1586 | 
 | 
|---|
 | 1587 |                                         PRINT(
 | 
|---|
 | 1588 |                                                 std::cerr << "  @ " << toType << " " << initAlt.designation << std::endl;
 | 
|---|
 | 1589 |                                         )
 | 
|---|
 | 1590 | 
 | 
|---|
| [2890212] | 1591 |                                         // It is possible that a cast can throw away some values in a multiply-valued
 | 
|---|
 | 1592 |                                         // expression, e.g. cast-to-void, one value to zero. Figure out the prefix of
 | 
|---|
 | 1593 |                                         // the subexpression results that are cast directly. The candidate is invalid
 | 
|---|
| [17a0ede2] | 1594 |                                         // if it has fewer results than there are types to cast to.
 | 
|---|
 | 1595 |                                         int discardedValues = cand->expr->result->size() - toType->size();
 | 
|---|
 | 1596 |                                         if ( discardedValues < 0 ) continue;
 | 
|---|
 | 1597 | 
 | 
|---|
 | 1598 |                                         // unification run for side-effects
 | 
|---|
| [bb87dd0] | 1599 |                                         bool canUnify = unify( toType, cand->expr->result, env, need, have, open, symtab );
 | 
|---|
| [0292aa4] | 1600 |                                         (void) canUnify;
 | 
|---|
| [ef9988b] | 1601 |                                         Cost thisCost = computeConversionCost( cand->expr->result, toType, cand->expr->get_lvalue(),
 | 
|---|
| [bb87dd0] | 1602 |                                                 symtab, env );
 | 
|---|
 | 1603 |                                         PRINT(
 | 
|---|
 | 1604 |                                                 Cost legacyCost = castCost( cand->expr->result, toType, cand->expr->get_lvalue(),
 | 
|---|
| [cf32116] | 1605 |                                                         symtab, env );
 | 
|---|
| [bb87dd0] | 1606 |                                                 std::cerr << "Considering initialization:";
 | 
|---|
 | 1607 |                                                 std::cerr << std::endl << "  FROM: " << cand->expr->result << std::endl;
 | 
|---|
 | 1608 |                                                 std::cerr << std::endl << "  TO: "   << toType             << std::endl;
 | 
|---|
 | 1609 |                                                 std::cerr << std::endl << "  Unification " << (canUnify ? "succeeded" : "failed");
 | 
|---|
 | 1610 |                                                 std::cerr << std::endl << "  Legacy cost " << legacyCost;
 | 
|---|
 | 1611 |                                                 std::cerr << std::endl << "  New cost " << thisCost;
 | 
|---|
 | 1612 |                                                 std::cerr << std::endl;
 | 
|---|
 | 1613 |                                         )
 | 
|---|
| [17a0ede2] | 1614 |                                         if ( thisCost != Cost::infinity ) {
 | 
|---|
 | 1615 |                                                 // count one safe conversion for each value that is thrown away
 | 
|---|
 | 1616 |                                                 thisCost.incSafe( discardedValues );
 | 
|---|
| [2890212] | 1617 |                                                 CandidateRef newCand = std::make_shared<Candidate>(
 | 
|---|
 | 1618 |                                                         new ast::InitExpr{
 | 
|---|
 | 1619 |                                                                 initExpr->location, restructureCast( cand->expr, toType ),
 | 
|---|
 | 1620 |                                                                 initAlt.designation },
 | 
|---|
| [ef9988b] | 1621 |                                                         move(env), move( open ), move( need ), cand->cost, thisCost );
 | 
|---|
| [17a0ede2] | 1622 |                                                 inferParameters( newCand, matches );
 | 
|---|
 | 1623 |                                         }
 | 
|---|
 | 1624 |                                 }
 | 
|---|
| [e5c3811] | 1625 | 
 | 
|---|
| [17a0ede2] | 1626 |                         }
 | 
|---|
 | 1627 | 
 | 
|---|
 | 1628 |                         // select first on argument cost, then conversion cost
 | 
|---|
 | 1629 |                         CandidateList minArgCost = findMinCost( matches );
 | 
|---|
 | 1630 |                         promoteCvtCost( minArgCost );
 | 
|---|
 | 1631 |                         candidates = findMinCost( minArgCost );
 | 
|---|
| [4b7cce6] | 1632 |                 }
 | 
|---|
 | 1633 | 
 | 
|---|
 | 1634 |                 void postvisit( const ast::InitExpr * ) {
 | 
|---|
 | 1635 |                         assertf( false, "CandidateFinder should never see a resolved InitExpr." );
 | 
|---|
 | 1636 |                 }
 | 
|---|
 | 1637 | 
 | 
|---|
 | 1638 |                 void postvisit( const ast::DeletedExpr * ) {
 | 
|---|
 | 1639 |                         assertf( false, "CandidateFinder should never see a DeletedExpr." );
 | 
|---|
 | 1640 |                 }
 | 
|---|
 | 1641 | 
 | 
|---|
 | 1642 |                 void postvisit( const ast::GenericExpr * ) {
 | 
|---|
 | 1643 |                         assertf( false, "_Generic is not yet supported." );
 | 
|---|
 | 1644 |                 }
 | 
|---|
| [396037d] | 1645 |         };
 | 
|---|
 | 1646 | 
 | 
|---|
| [0d070ca] | 1647 |         // size_t Finder::traceId = Stats::Heap::new_stacktrace_id("Finder");
 | 
|---|
| [2890212] | 1648 |         /// Prunes a list of candidates down to those that have the minimum conversion cost for a given
 | 
|---|
| [396037d] | 1649 |         /// return type. Skips ambiguous candidates.
 | 
|---|
| [d57e349] | 1650 | 
 | 
|---|
| [1389810] | 1651 | } // anonymous namespace
 | 
|---|
| [d57e349] | 1652 | 
 | 
|---|
| [1389810] | 1653 | bool CandidateFinder::pruneCandidates( CandidateList & candidates, CandidateList & out, std::vector<std::string> & errors ) {
 | 
|---|
 | 1654 |         struct PruneStruct {
 | 
|---|
 | 1655 |                 CandidateRef candidate;
 | 
|---|
 | 1656 |                 bool ambiguous;
 | 
|---|
 | 1657 | 
 | 
|---|
 | 1658 |                 PruneStruct() = default;
 | 
|---|
 | 1659 |                 PruneStruct( const CandidateRef & c ) : candidate( c ), ambiguous( false ) {}
 | 
|---|
 | 1660 |         };
 | 
|---|
 | 1661 | 
 | 
|---|
 | 1662 |         // find lowest-cost candidate for each type
 | 
|---|
 | 1663 |         std::unordered_map< std::string, PruneStruct > selected;
 | 
|---|
 | 1664 |         // attempt to skip satisfyAssertions on more expensive alternatives if better options have been found
 | 
|---|
 | 1665 |         std::sort(candidates.begin(), candidates.end(), [](const CandidateRef & x, const CandidateRef & y){return x->cost < y->cost;});
 | 
|---|
 | 1666 |         for ( CandidateRef & candidate : candidates ) {
 | 
|---|
 | 1667 |                 std::string mangleName;
 | 
|---|
 | 1668 |                 {
 | 
|---|
 | 1669 |                         ast::ptr< ast::Type > newType = candidate->expr->result;
 | 
|---|
 | 1670 |                         assertf(candidate->expr->result, "Result of expression %p for candidate is null", candidate->expr.get());
 | 
|---|
 | 1671 |                         candidate->env.apply( newType );
 | 
|---|
 | 1672 |                         mangleName = Mangle::mangle( newType );
 | 
|---|
 | 1673 |                 }
 | 
|---|
 | 1674 | 
 | 
|---|
 | 1675 |                 auto found = selected.find( mangleName );
 | 
|---|
 | 1676 |                 if (found != selected.end() && found->second.candidate->cost < candidate->cost) {
 | 
|---|
 | 1677 |                         PRINT(
 | 
|---|
 | 1678 |                                 std::cerr << "cost " << candidate->cost << " loses to "
 | 
|---|
 | 1679 |                                         << found->second.candidate->cost << std::endl;
 | 
|---|
 | 1680 |                         )
 | 
|---|
 | 1681 |                         continue;
 | 
|---|
 | 1682 |                 }
 | 
|---|
 | 1683 | 
 | 
|---|
 | 1684 |                 // xxx - when do satisfyAssertions produce more than 1 result?
 | 
|---|
 | 1685 |                 // this should only happen when initial result type contains
 | 
|---|
 | 1686 |                 // unbound type parameters, then it should never be pruned by
 | 
|---|
 | 1687 |                 // the previous step, since renameTyVars guarantees the mangled name
 | 
|---|
 | 1688 |                 // is unique.
 | 
|---|
 | 1689 |                 CandidateList satisfied;
 | 
|---|
| [e3282fe] | 1690 |                 bool needRecomputeKey = false;
 | 
|---|
 | 1691 |                 if (candidate->need.empty()) {
 | 
|---|
 | 1692 |                         satisfied.emplace_back(candidate);
 | 
|---|
 | 1693 |                 }
 | 
|---|
 | 1694 |                 else {
 | 
|---|
 | 1695 |                         satisfyAssertions(candidate, localSyms, satisfied, errors);
 | 
|---|
 | 1696 |                         needRecomputeKey = true;
 | 
|---|
 | 1697 |                 }
 | 
|---|
| [1389810] | 1698 | 
 | 
|---|
 | 1699 |                 for (auto & newCand : satisfied) {
 | 
|---|
 | 1700 |                         // recomputes type key, if satisfyAssertions changed it
 | 
|---|
| [e3282fe] | 1701 |                         if (needRecomputeKey)
 | 
|---|
| [d57e349] | 1702 |                         {
 | 
|---|
| [1389810] | 1703 |                                 ast::ptr< ast::Type > newType = newCand->expr->result;
 | 
|---|
 | 1704 |                                 assertf(newCand->expr->result, "Result of expression %p for candidate is null", newCand->expr.get());
 | 
|---|
 | 1705 |                                 newCand->env.apply( newType );
 | 
|---|
| [d57e349] | 1706 |                                 mangleName = Mangle::mangle( newType );
 | 
|---|
 | 1707 |                         }
 | 
|---|
 | 1708 |                         auto found = selected.find( mangleName );
 | 
|---|
 | 1709 |                         if ( found != selected.end() ) {
 | 
|---|
| [1389810] | 1710 |                                 if ( newCand->cost < found->second.candidate->cost ) {
 | 
|---|
| [d57e349] | 1711 |                                         PRINT(
 | 
|---|
| [1389810] | 1712 |                                                 std::cerr << "cost " << newCand->cost << " beats "
 | 
|---|
| [d57e349] | 1713 |                                                         << found->second.candidate->cost << std::endl;
 | 
|---|
 | 1714 |                                         )
 | 
|---|
 | 1715 | 
 | 
|---|
| [1389810] | 1716 |                                         found->second = PruneStruct{ newCand };
 | 
|---|
 | 1717 |                                 } else if ( newCand->cost == found->second.candidate->cost ) {
 | 
|---|
| [2890212] | 1718 |                                         // if one of the candidates contains a deleted identifier, can pick the other,
 | 
|---|
 | 1719 |                                         // since deleted expressions should not be ambiguous if there is another option
 | 
|---|
| [d57e349] | 1720 |                                         // that is at least as good
 | 
|---|
| [1389810] | 1721 |                                         if ( findDeletedExpr( newCand->expr ) ) {
 | 
|---|
| [d57e349] | 1722 |                                                 // do nothing
 | 
|---|
 | 1723 |                                                 PRINT( std::cerr << "candidate is deleted" << std::endl; )
 | 
|---|
 | 1724 |                                         } else if ( findDeletedExpr( found->second.candidate->expr ) ) {
 | 
|---|
 | 1725 |                                                 PRINT( std::cerr << "current is deleted" << std::endl; )
 | 
|---|
| [1389810] | 1726 |                                                 found->second = PruneStruct{ newCand };
 | 
|---|
| [d57e349] | 1727 |                                         } else {
 | 
|---|
 | 1728 |                                                 PRINT( std::cerr << "marking ambiguous" << std::endl; )
 | 
|---|
 | 1729 |                                                 found->second.ambiguous = true;
 | 
|---|
 | 1730 |                                         }
 | 
|---|
| [943bfad] | 1731 |                                 } else {
 | 
|---|
| [1389810] | 1732 |                                         // xxx - can satisfyAssertions increase the cost?
 | 
|---|
| [d57e349] | 1733 |                                         PRINT(
 | 
|---|
| [1389810] | 1734 |                                                 std::cerr << "cost " << newCand->cost << " loses to "
 | 
|---|
| [d57e349] | 1735 |                                                         << found->second.candidate->cost << std::endl;
 | 
|---|
| [943bfad] | 1736 |                                         )
 | 
|---|
| [d57e349] | 1737 |                                 }
 | 
|---|
 | 1738 |                         } else {
 | 
|---|
| [1389810] | 1739 |                                 selected.emplace_hint( found, mangleName, newCand );
 | 
|---|
| [d57e349] | 1740 |                         }
 | 
|---|
 | 1741 |                 }
 | 
|---|
| [1389810] | 1742 |         }
 | 
|---|
| [d57e349] | 1743 | 
 | 
|---|
| [1389810] | 1744 |         // report unambiguous min-cost candidates
 | 
|---|
 | 1745 |         // CandidateList out;
 | 
|---|
 | 1746 |         for ( auto & target : selected ) {
 | 
|---|
 | 1747 |                 if ( target.second.ambiguous ) continue;
 | 
|---|
| [d57e349] | 1748 | 
 | 
|---|
| [1389810] | 1749 |                 CandidateRef cand = target.second.candidate;
 | 
|---|
| [2890212] | 1750 | 
 | 
|---|
| [1389810] | 1751 |                 ast::ptr< ast::Type > newResult = cand->expr->result;
 | 
|---|
 | 1752 |                 cand->env.applyFree( newResult );
 | 
|---|
 | 1753 |                 cand->expr = ast::mutate_field(
 | 
|---|
 | 1754 |                         cand->expr.get(), &ast::Expr::result, move( newResult ) );
 | 
|---|
| [2890212] | 1755 | 
 | 
|---|
| [1389810] | 1756 |                 out.emplace_back( cand );
 | 
|---|
| [d57e349] | 1757 |         }
 | 
|---|
| [1389810] | 1758 |         // if everything is lost in satisfyAssertions, report the error
 | 
|---|
 | 1759 |         return !selected.empty();
 | 
|---|
 | 1760 | }
 | 
|---|
| [396037d] | 1761 | 
 | 
|---|
| [99d4584] | 1762 | void CandidateFinder::find( const ast::Expr * expr, ResolvMode mode ) {
 | 
|---|
| [396037d] | 1763 |         // Find alternatives for expression
 | 
|---|
 | 1764 |         ast::Pass<Finder> finder{ *this };
 | 
|---|
 | 1765 |         expr->accept( finder );
 | 
|---|
 | 1766 | 
 | 
|---|
 | 1767 |         if ( mode.failFast && candidates.empty() ) {
 | 
|---|
| [7ff3e522] | 1768 |                 switch(finder.core.reason.code) {
 | 
|---|
| [71d6bd8] | 1769 |                 case Finder::NotFound:
 | 
|---|
 | 1770 |                         { SemanticError( expr, "No alternatives for expression " ); break; }
 | 
|---|
 | 1771 |                 case Finder::NoMatch:
 | 
|---|
 | 1772 |                         { SemanticError( expr, "Invalid application of existing declaration(s) in expression " ); break; }
 | 
|---|
 | 1773 |                 case Finder::ArgsToFew:
 | 
|---|
 | 1774 |                 case Finder::ArgsToMany:
 | 
|---|
 | 1775 |                 case Finder::RetsToFew:
 | 
|---|
 | 1776 |                 case Finder::RetsToMany:
 | 
|---|
 | 1777 |                 case Finder::NoReason:
 | 
|---|
 | 1778 |                 default:
 | 
|---|
 | 1779 |                         { SemanticError( expr->location, "No reasonable alternatives for expression : reasons unkown" ); }
 | 
|---|
 | 1780 |                 }
 | 
|---|
| [396037d] | 1781 |         }
 | 
|---|
 | 1782 | 
 | 
|---|
| [1389810] | 1783 |         /*
 | 
|---|
| [396037d] | 1784 |         if ( mode.satisfyAssns || mode.prune ) {
 | 
|---|
 | 1785 |                 // trim candidates to just those where the assertions are satisfiable
 | 
|---|
 | 1786 |                 // - necessary pre-requisite to pruning
 | 
|---|
 | 1787 |                 CandidateList satisfied;
 | 
|---|
 | 1788 |                 std::vector< std::string > errors;
 | 
|---|
| [b69233ac] | 1789 |                 for ( CandidateRef & candidate : candidates ) {
 | 
|---|
| [9ea38de] | 1790 |                         satisfyAssertions( candidate, localSyms, satisfied, errors );
 | 
|---|
| [396037d] | 1791 |                 }
 | 
|---|
 | 1792 | 
 | 
|---|
 | 1793 |                 // fail early if none such
 | 
|---|
 | 1794 |                 if ( mode.failFast && satisfied.empty() ) {
 | 
|---|
 | 1795 |                         std::ostringstream stream;
 | 
|---|
 | 1796 |                         stream << "No alternatives with satisfiable assertions for " << expr << "\n";
 | 
|---|
 | 1797 |                         for ( const auto& err : errors ) {
 | 
|---|
 | 1798 |                                 stream << err;
 | 
|---|
 | 1799 |                         }
 | 
|---|
 | 1800 |                         SemanticError( expr->location, stream.str() );
 | 
|---|
 | 1801 |                 }
 | 
|---|
 | 1802 | 
 | 
|---|
 | 1803 |                 // reset candidates
 | 
|---|
| [9d5089e] | 1804 |                 candidates = move( satisfied );
 | 
|---|
| [396037d] | 1805 |         }
 | 
|---|
| [1389810] | 1806 |         */
 | 
|---|
| [396037d] | 1807 | 
 | 
|---|
 | 1808 |         if ( mode.prune ) {
 | 
|---|
 | 1809 |                 // trim candidates to single best one
 | 
|---|
 | 1810 |                 PRINT(
 | 
|---|
 | 1811 |                         std::cerr << "alternatives before prune:" << std::endl;
 | 
|---|
 | 1812 |                         print( std::cerr, candidates );
 | 
|---|
 | 1813 |                 )
 | 
|---|
 | 1814 | 
 | 
|---|
| [1389810] | 1815 |                 CandidateList pruned;
 | 
|---|
 | 1816 |                 std::vector<std::string> errors;
 | 
|---|
 | 1817 |                 bool found = pruneCandidates( candidates, pruned, errors );
 | 
|---|
| [2890212] | 1818 | 
 | 
|---|
| [396037d] | 1819 |                 if ( mode.failFast && pruned.empty() ) {
 | 
|---|
 | 1820 |                         std::ostringstream stream;
 | 
|---|
| [943bfad] | 1821 |                         if (found) {
 | 
|---|
| [1389810] | 1822 |                                 CandidateList winners = findMinCost( candidates );
 | 
|---|
 | 1823 |                                 stream << "Cannot choose between " << winners.size() << " alternatives for "
 | 
|---|
 | 1824 |                                         "expression\n";
 | 
|---|
 | 1825 |                                 ast::print( stream, expr );
 | 
|---|
 | 1826 |                                 stream << " Alternatives are:\n";
 | 
|---|
 | 1827 |                                 print( stream, winners, 1 );
 | 
|---|
 | 1828 |                                 SemanticError( expr->location, stream.str() );
 | 
|---|
 | 1829 |                         }
 | 
|---|
 | 1830 |                         else {
 | 
|---|
 | 1831 |                                 stream << "No alternatives with satisfiable assertions for " << expr << "\n";
 | 
|---|
 | 1832 |                                 for ( const auto& err : errors ) {
 | 
|---|
 | 1833 |                                         stream << err;
 | 
|---|
 | 1834 |                                 }
 | 
|---|
 | 1835 |                                 SemanticError( expr->location, stream.str() );
 | 
|---|
 | 1836 |                         }
 | 
|---|
| [396037d] | 1837 |                 }
 | 
|---|
| [d57e349] | 1838 | 
 | 
|---|
 | 1839 |                 auto oldsize = candidates.size();
 | 
|---|
| [9d5089e] | 1840 |                 candidates = move( pruned );
 | 
|---|
| [d57e349] | 1841 | 
 | 
|---|
 | 1842 |                 PRINT(
 | 
|---|
 | 1843 |                         std::cerr << "there are " << oldsize << " alternatives before elimination" << std::endl;
 | 
|---|
 | 1844 |                 )
 | 
|---|
 | 1845 |                 PRINT(
 | 
|---|
| [2890212] | 1846 |                         std::cerr << "there are " << candidates.size() << " alternatives after elimination"
 | 
|---|
| [d57e349] | 1847 |                                 << std::endl;
 | 
|---|
 | 1848 |                 )
 | 
|---|
| [396037d] | 1849 |         }
 | 
|---|
 | 1850 | 
 | 
|---|
| [2890212] | 1851 |         // adjust types after pruning so that types substituted by pruneAlternatives are correctly
 | 
|---|
| [d57e349] | 1852 |         // adjusted
 | 
|---|
 | 1853 |         if ( mode.adjust ) {
 | 
|---|
 | 1854 |                 for ( CandidateRef & r : candidates ) {
 | 
|---|
| [2890212] | 1855 |                         r->expr = ast::mutate_field(
 | 
|---|
 | 1856 |                                 r->expr.get(), &ast::Expr::result,
 | 
|---|
| [9ea38de] | 1857 |                                 adjustExprType( r->expr->result, r->env, localSyms ) );
 | 
|---|
| [d57e349] | 1858 |                 }
 | 
|---|
 | 1859 |         }
 | 
|---|
 | 1860 | 
 | 
|---|
 | 1861 |         // Central location to handle gcc extension keyword, etc. for all expressions
 | 
|---|
 | 1862 |         for ( CandidateRef & r : candidates ) {
 | 
|---|
 | 1863 |                 if ( r->expr->extension != expr->extension ) {
 | 
|---|
 | 1864 |                         r->expr.get_and_mutate()->extension = expr->extension;
 | 
|---|
 | 1865 |                 }
 | 
|---|
 | 1866 |         }
 | 
|---|
| [99d4584] | 1867 | }
 | 
|---|
 | 1868 | 
 | 
|---|
| [2890212] | 1869 | std::vector< CandidateFinder > CandidateFinder::findSubExprs(
 | 
|---|
 | 1870 |         const std::vector< ast::ptr< ast::Expr > > & xs
 | 
|---|
| [2773ab8] | 1871 | ) {
 | 
|---|
 | 1872 |         std::vector< CandidateFinder > out;
 | 
|---|
 | 1873 | 
 | 
|---|
| [396037d] | 1874 |         for ( const auto & x : xs ) {
 | 
|---|
| [9ea38de] | 1875 |                 out.emplace_back( localSyms, env );
 | 
|---|
| [396037d] | 1876 |                 out.back().find( x, ResolvMode::withAdjustment() );
 | 
|---|
| [2890212] | 1877 | 
 | 
|---|
| [396037d] | 1878 |                 PRINT(
 | 
|---|
 | 1879 |                         std::cerr << "findSubExprs" << std::endl;
 | 
|---|
 | 1880 |                         print( std::cerr, out.back().candidates );
 | 
|---|
 | 1881 |                 )
 | 
|---|
 | 1882 |         }
 | 
|---|
| [2773ab8] | 1883 | 
 | 
|---|
 | 1884 |         return out;
 | 
|---|
 | 1885 | }
 | 
|---|
 | 1886 | 
 | 
|---|
| [99d4584] | 1887 | } // namespace ResolvExpr
 | 
|---|
 | 1888 | 
 | 
|---|
 | 1889 | // Local Variables: //
 | 
|---|
 | 1890 | // tab-width: 4 //
 | 
|---|
 | 1891 | // mode: c++ //
 | 
|---|
 | 1892 | // compile-command: "make install" //
 | 
|---|
 | 1893 | // End: //
 | 
|---|