[a32b204] | 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 | //
|
---|
[6ed1d4b] | 7 | // AlternativeFinder.cc --
|
---|
[a32b204] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sat May 16 23:52:08 2015
|
---|
[e04ef3a] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[8e9cbb2] | 12 | // Last Modified On : Mon Jul 4 17:02:51 2016
|
---|
| 13 | // Update Count : 29
|
---|
[a32b204] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 16 | #include <list>
|
---|
| 17 | #include <iterator>
|
---|
| 18 | #include <algorithm>
|
---|
| 19 | #include <functional>
|
---|
| 20 | #include <cassert>
|
---|
[ebf5689] | 21 | #include <unordered_map>
|
---|
| 22 | #include <utility>
|
---|
| 23 | #include <vector>
|
---|
[51b73452] | 24 |
|
---|
| 25 | #include "AlternativeFinder.h"
|
---|
| 26 | #include "Alternative.h"
|
---|
| 27 | #include "Cost.h"
|
---|
| 28 | #include "typeops.h"
|
---|
| 29 | #include "Unify.h"
|
---|
| 30 | #include "RenameVars.h"
|
---|
| 31 | #include "SynTree/Type.h"
|
---|
| 32 | #include "SynTree/Declaration.h"
|
---|
| 33 | #include "SynTree/Expression.h"
|
---|
| 34 | #include "SynTree/Initializer.h"
|
---|
| 35 | #include "SynTree/Visitor.h"
|
---|
| 36 | #include "SymTab/Indexer.h"
|
---|
| 37 | #include "SymTab/Mangler.h"
|
---|
| 38 | #include "SynTree/TypeSubstitution.h"
|
---|
| 39 | #include "SymTab/Validate.h"
|
---|
[6eb8948] | 40 | #include "Tuples/Tuples.h"
|
---|
[d3b7937] | 41 | #include "Common/utility.h"
|
---|
[70f89d00] | 42 | #include "InitTweak/InitTweak.h"
|
---|
[85517ddb] | 43 | #include "ResolveTypeof.h"
|
---|
[51b73452] | 44 |
|
---|
[b87a5ed] | 45 | extern bool resolvep;
|
---|
[6ed1d4b] | 46 | #define PRINT( text ) if ( resolvep ) { text }
|
---|
[51b73452] | 47 | //#define DEBUG_COST
|
---|
| 48 |
|
---|
| 49 | namespace ResolvExpr {
|
---|
[a32b204] | 50 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) {
|
---|
| 51 | CastExpr *castToVoid = new CastExpr( expr );
|
---|
| 52 |
|
---|
| 53 | AlternativeFinder finder( indexer, env );
|
---|
| 54 | finder.findWithAdjustment( castToVoid );
|
---|
| 55 |
|
---|
| 56 | // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0
|
---|
| 57 | // interpretations, an exception has already been thrown.
|
---|
| 58 | assert( finder.get_alternatives().size() == 1 );
|
---|
| 59 | CastExpr *newExpr = dynamic_cast< CastExpr* >( finder.get_alternatives().front().expr );
|
---|
| 60 | assert( newExpr );
|
---|
| 61 | env = finder.get_alternatives().front().env;
|
---|
| 62 | return newExpr->get_arg()->clone();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[908cc83] | 65 | Cost sumCost( const AltList &in ) {
|
---|
| 66 | Cost total;
|
---|
| 67 | for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) {
|
---|
| 68 | total += i->cost;
|
---|
| 69 | }
|
---|
| 70 | return total;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[a32b204] | 73 | namespace {
|
---|
| 74 | void printAlts( const AltList &list, std::ostream &os, int indent = 0 ) {
|
---|
| 75 | for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {
|
---|
| 76 | i->print( os, indent );
|
---|
| 77 | os << std::endl;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[d9a0e76] | 80 |
|
---|
[a32b204] | 81 | void makeExprList( const AltList &in, std::list< Expression* > &out ) {
|
---|
| 82 | for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) {
|
---|
| 83 | out.push_back( i->expr->clone() );
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
[d9a0e76] | 86 |
|
---|
[a32b204] | 87 | struct PruneStruct {
|
---|
| 88 | bool isAmbiguous;
|
---|
| 89 | AltList::iterator candidate;
|
---|
| 90 | PruneStruct() {}
|
---|
| 91 | PruneStruct( AltList::iterator candidate ): isAmbiguous( false ), candidate( candidate ) {}
|
---|
| 92 | };
|
---|
| 93 |
|
---|
[0f19d763] | 94 | /// Prunes a list of alternatives down to those that have the minimum conversion cost for a given return type; skips ambiguous interpretations
|
---|
[a32b204] | 95 | template< typename InputIterator, typename OutputIterator >
|
---|
| 96 | void pruneAlternatives( InputIterator begin, InputIterator end, OutputIterator out, const SymTab::Indexer &indexer ) {
|
---|
| 97 | // select the alternatives that have the minimum conversion cost for a particular set of result types
|
---|
| 98 | std::map< std::string, PruneStruct > selected;
|
---|
| 99 | for ( AltList::iterator candidate = begin; candidate != end; ++candidate ) {
|
---|
| 100 | PruneStruct current( candidate );
|
---|
| 101 | std::string mangleName;
|
---|
[906e24d] | 102 | {
|
---|
| 103 | Type * newType = candidate->expr->get_result()->clone();
|
---|
[a32b204] | 104 | candidate->env.apply( newType );
|
---|
[906e24d] | 105 | mangleName = SymTab::Mangler::mangle( newType );
|
---|
[a32b204] | 106 | delete newType;
|
---|
| 107 | }
|
---|
| 108 | std::map< std::string, PruneStruct >::iterator mapPlace = selected.find( mangleName );
|
---|
| 109 | if ( mapPlace != selected.end() ) {
|
---|
| 110 | if ( candidate->cost < mapPlace->second.candidate->cost ) {
|
---|
| 111 | PRINT(
|
---|
[6ed1d4b] | 112 | std::cerr << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl;
|
---|
[7c64920] | 113 | )
|
---|
[0f19d763] | 114 | selected[ mangleName ] = current;
|
---|
[a32b204] | 115 | } else if ( candidate->cost == mapPlace->second.candidate->cost ) {
|
---|
| 116 | PRINT(
|
---|
[6ed1d4b] | 117 | std::cerr << "marking ambiguous" << std::endl;
|
---|
[7c64920] | 118 | )
|
---|
[0f19d763] | 119 | mapPlace->second.isAmbiguous = true;
|
---|
[a32b204] | 120 | }
|
---|
| 121 | } else {
|
---|
| 122 | selected[ mangleName ] = current;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
[d9a0e76] | 125 |
|
---|
| 126 | PRINT(
|
---|
[6ed1d4b] | 127 | std::cerr << "there are " << selected.size() << " alternatives before elimination" << std::endl;
|
---|
[7c64920] | 128 | )
|
---|
[a32b204] | 129 |
|
---|
[0f19d763] | 130 | // accept the alternatives that were unambiguous
|
---|
| 131 | for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) {
|
---|
| 132 | if ( ! target->second.isAmbiguous ) {
|
---|
| 133 | Alternative &alt = *target->second.candidate;
|
---|
[906e24d] | 134 | alt.env.applyFree( alt.expr->get_result() );
|
---|
[0f19d763] | 135 | *out++ = alt;
|
---|
[a32b204] | 136 | }
|
---|
[0f19d763] | 137 | }
|
---|
[d9a0e76] | 138 | }
|
---|
[a32b204] | 139 |
|
---|
| 140 | template< typename InputIterator >
|
---|
| 141 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
|
---|
| 142 | while ( begin != end ) {
|
---|
| 143 | result.simpleCombine( (*begin++).env );
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
[d9a0e76] | 146 |
|
---|
[a32b204] | 147 | void renameTypes( Expression *expr ) {
|
---|
[906e24d] | 148 | expr->get_result()->accept( global_renamer );
|
---|
[e76acbe] | 149 | }
|
---|
[d9a0e76] | 150 | }
|
---|
| 151 |
|
---|
[a32b204] | 152 | template< typename InputIterator, typename OutputIterator >
|
---|
| 153 | void AlternativeFinder::findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ) {
|
---|
| 154 | while ( begin != end ) {
|
---|
| 155 | AlternativeFinder finder( indexer, env );
|
---|
| 156 | finder.findWithAdjustment( *begin );
|
---|
| 157 | // XXX either this
|
---|
| 158 | //Designators::fixDesignations( finder, (*begin++)->get_argName() );
|
---|
| 159 | // or XXX this
|
---|
| 160 | begin++;
|
---|
| 161 | PRINT(
|
---|
[6ed1d4b] | 162 | std::cerr << "findSubExprs" << std::endl;
|
---|
| 163 | printAlts( finder.alternatives, std::cerr );
|
---|
[7c64920] | 164 | )
|
---|
[0f19d763] | 165 | *out++ = finder;
|
---|
[a32b204] | 166 | }
|
---|
[d9a0e76] | 167 | }
|
---|
| 168 |
|
---|
[a32b204] | 169 | AlternativeFinder::AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env )
|
---|
| 170 | : indexer( indexer ), env( env ) {
|
---|
[d9a0e76] | 171 | }
|
---|
[51b73452] | 172 |
|
---|
[b6fe7e6] | 173 | void AlternativeFinder::find( Expression *expr, bool adjust, bool prune ) {
|
---|
[a32b204] | 174 | expr->accept( *this );
|
---|
| 175 | if ( alternatives.empty() ) {
|
---|
| 176 | throw SemanticError( "No reasonable alternatives for expression ", expr );
|
---|
| 177 | }
|
---|
| 178 | for ( AltList::iterator i = alternatives.begin(); i != alternatives.end(); ++i ) {
|
---|
| 179 | if ( adjust ) {
|
---|
[906e24d] | 180 | adjustExprType( i->expr->get_result(), i->env, indexer );
|
---|
[a32b204] | 181 | }
|
---|
| 182 | }
|
---|
[b6fe7e6] | 183 | if ( prune ) {
|
---|
| 184 | PRINT(
|
---|
| 185 | std::cerr << "alternatives before prune:" << std::endl;
|
---|
| 186 | printAlts( alternatives, std::cerr );
|
---|
| 187 | )
|
---|
| 188 | AltList::iterator oldBegin = alternatives.begin();
|
---|
| 189 | pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer );
|
---|
| 190 | if ( alternatives.begin() == oldBegin ) {
|
---|
| 191 | std::ostringstream stream;
|
---|
| 192 | stream << "Can't choose between alternatives for expression ";
|
---|
| 193 | expr->print( stream );
|
---|
| 194 | stream << "Alternatives are:";
|
---|
| 195 | AltList winners;
|
---|
| 196 | findMinCost( alternatives.begin(), alternatives.end(), back_inserter( winners ) );
|
---|
| 197 | printAlts( winners, stream, 8 );
|
---|
| 198 | throw SemanticError( stream.str() );
|
---|
| 199 | }
|
---|
| 200 | alternatives.erase( oldBegin, alternatives.end() );
|
---|
| 201 | PRINT(
|
---|
| 202 | std::cerr << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
|
---|
| 203 | )
|
---|
[a32b204] | 204 | }
|
---|
[8e9cbb2] | 205 |
|
---|
| 206 | // Central location to handle gcc extension keyword for all expression types.
|
---|
| 207 | for ( Alternative &iter: alternatives ) {
|
---|
| 208 | iter.expr->set_extension( expr->get_extension() );
|
---|
| 209 | } // for
|
---|
[0f19d763] | 210 | }
|
---|
[d9a0e76] | 211 |
|
---|
[b6fe7e6] | 212 | void AlternativeFinder::findWithAdjustment( Expression *expr, bool prune ) {
|
---|
| 213 | find( expr, true, prune );
|
---|
[d9a0e76] | 214 | }
|
---|
[a32b204] | 215 |
|
---|
| 216 | template< typename StructOrUnionType >
|
---|
[add7117] | 217 | void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
|
---|
[5af62f1] | 218 |
|
---|
| 219 | // // member must be either a tuple expression or a name expr
|
---|
| 220 | // if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( memberExpr->get_member() ) ) {
|
---|
| 221 | // addAggMembers( structInst, agg->expr, agg->cost, nameExpr->get_name() );
|
---|
| 222 | // } else {
|
---|
| 223 | // TupleExpr * tupleExpr = safe_dynamic_cast< TupleExpr * >( memberExpr->get_member() );
|
---|
| 224 | // // xxx - ...
|
---|
| 225 | // assert( false );
|
---|
| 226 | // }
|
---|
| 227 | // if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( memberExpr->get_member() ) ) {
|
---|
| 228 |
|
---|
| 229 | // }
|
---|
[3b58d91] | 230 | NameExpr * nameExpr = safe_dynamic_cast< NameExpr * >( member );
|
---|
| 231 | const std::string & name = nameExpr->get_name();
|
---|
[a32b204] | 232 | std::list< Declaration* > members;
|
---|
| 233 | aggInst->lookup( name, members );
|
---|
| 234 | for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
|
---|
| 235 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
|
---|
[79970ed] | 236 | alternatives.push_back( Alternative( new MemberExpr( dwt, expr->clone() ), env, newCost ) );
|
---|
[a32b204] | 237 | renameTypes( alternatives.back().expr );
|
---|
| 238 | } else {
|
---|
| 239 | assert( false );
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
[d9a0e76] | 242 | }
|
---|
[a32b204] | 243 |
|
---|
| 244 | void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) {
|
---|
| 245 | alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) );
|
---|
[d9a0e76] | 246 | }
|
---|
| 247 |
|
---|
[a32b204] | 248 | Cost computeConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) {
|
---|
[8f7cea1] | 249 | ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( alt.expr );
|
---|
[906e24d] | 250 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
|
---|
[8f7cea1] | 251 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
|
---|
[a32b204] | 252 |
|
---|
| 253 | Cost convCost( 0, 0, 0 );
|
---|
| 254 | std::list< DeclarationWithType* >& formals = function->get_parameters();
|
---|
| 255 | std::list< DeclarationWithType* >::iterator formal = formals.begin();
|
---|
| 256 | std::list< Expression* >& actuals = appExpr->get_args();
|
---|
[0362d42] | 257 |
|
---|
[e76acbe] | 258 | std::list< Type * > formalTypes;
|
---|
| 259 | std::list< Type * >::iterator formalType = formalTypes.end();
|
---|
| 260 |
|
---|
[a32b204] | 261 | for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
|
---|
[e76acbe] | 262 |
|
---|
[a32b204] | 263 | PRINT(
|
---|
[6ed1d4b] | 264 | std::cerr << "actual expression:" << std::endl;
|
---|
| 265 | (*actualExpr)->print( std::cerr, 8 );
|
---|
| 266 | std::cerr << "--- results are" << std::endl;
|
---|
[906e24d] | 267 | (*actualExpr)->get_result()->print( std::cerr, 8 );
|
---|
[7c64920] | 268 | )
|
---|
| 269 | std::list< DeclarationWithType* >::iterator startFormal = formal;
|
---|
[a32b204] | 270 | Cost actualCost;
|
---|
[906e24d] | 271 | std::list< Type * > flatActualTypes;
|
---|
| 272 | flatten( (*actualExpr)->get_result(), back_inserter( flatActualTypes ) );
|
---|
[aa8f9df] | 273 | for ( std::list< Type* >::iterator actualType = flatActualTypes.begin(); actualType != flatActualTypes.end(); ++actualType ) {
|
---|
| 274 |
|
---|
[e76acbe] | 275 |
|
---|
| 276 | // tuple handling code
|
---|
| 277 | if ( formalType == formalTypes.end() ) {
|
---|
| 278 | // the type of the formal parameter may be a tuple type. To make this easier to work with,
|
---|
| 279 | // flatten the tuple type and traverse the resulting list of types, incrementing the formal
|
---|
| 280 | // iterator once its types have been extracted. Once a particular formal parameter's type has
|
---|
| 281 | // been exhausted load the next formal parameter's type.
|
---|
| 282 | if ( formal == formals.end() ) {
|
---|
| 283 | if ( function->get_isVarArgs() ) {
|
---|
| 284 | convCost += Cost( 1, 0, 0 );
|
---|
| 285 | break;
|
---|
| 286 | } else {
|
---|
| 287 | return Cost::infinity;
|
---|
| 288 | }
|
---|
[a32b204] | 289 | }
|
---|
[e76acbe] | 290 | formalTypes.clear();
|
---|
| 291 | flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
|
---|
| 292 | formalType = formalTypes.begin();
|
---|
| 293 | ++formal;
|
---|
[a32b204] | 294 | }
|
---|
[e76acbe] | 295 |
|
---|
[a32b204] | 296 | PRINT(
|
---|
[6ed1d4b] | 297 | std::cerr << std::endl << "converting ";
|
---|
[0362d42] | 298 | (*actualType)->print( std::cerr, 8 );
|
---|
[6ed1d4b] | 299 | std::cerr << std::endl << " to ";
|
---|
| 300 | (*formal)->get_type()->print( std::cerr, 8 );
|
---|
[7c64920] | 301 | )
|
---|
[e76acbe] | 302 | Cost newCost = conversionCost( *actualType, *formalType, indexer, alt.env );
|
---|
[a32b204] | 303 | PRINT(
|
---|
[6ed1d4b] | 304 | std::cerr << std::endl << "cost is" << newCost << std::endl;
|
---|
[7c64920] | 305 | )
|
---|
[a32b204] | 306 |
|
---|
[7c64920] | 307 | if ( newCost == Cost::infinity ) {
|
---|
| 308 | return newCost;
|
---|
| 309 | }
|
---|
[a32b204] | 310 | convCost += newCost;
|
---|
| 311 | actualCost += newCost;
|
---|
| 312 |
|
---|
[e76acbe] | 313 | convCost += Cost( 0, polyCost( *formalType, alt.env, indexer ) + polyCost( *actualType, alt.env, indexer ), 0 );
|
---|
[a32b204] | 314 |
|
---|
[e76acbe] | 315 | formalType++;
|
---|
[a32b204] | 316 | }
|
---|
| 317 | if ( actualCost != Cost( 0, 0, 0 ) ) {
|
---|
| 318 | std::list< DeclarationWithType* >::iterator startFormalPlusOne = startFormal;
|
---|
| 319 | startFormalPlusOne++;
|
---|
| 320 | if ( formal == startFormalPlusOne ) {
|
---|
| 321 | // not a tuple type
|
---|
| 322 | Type *newType = (*startFormal)->get_type()->clone();
|
---|
| 323 | alt.env.apply( newType );
|
---|
| 324 | *actualExpr = new CastExpr( *actualExpr, newType );
|
---|
| 325 | } else {
|
---|
| 326 | TupleType *newType = new TupleType( Type::Qualifiers() );
|
---|
| 327 | for ( std::list< DeclarationWithType* >::iterator i = startFormal; i != formal; ++i ) {
|
---|
| 328 | newType->get_types().push_back( (*i)->get_type()->clone() );
|
---|
| 329 | }
|
---|
| 330 | alt.env.apply( newType );
|
---|
| 331 | *actualExpr = new CastExpr( *actualExpr, newType );
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[d9a0e76] | 335 | }
|
---|
[a32b204] | 336 | if ( formal != formals.end() ) {
|
---|
| 337 | return Cost::infinity;
|
---|
[d9a0e76] | 338 | }
|
---|
| 339 |
|
---|
[a32b204] | 340 | for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) {
|
---|
| 341 | PRINT(
|
---|
[6ed1d4b] | 342 | std::cerr << std::endl << "converting ";
|
---|
| 343 | assert->second.actualType->print( std::cerr, 8 );
|
---|
| 344 | std::cerr << std::endl << " to ";
|
---|
| 345 | assert->second.formalType->print( std::cerr, 8 );
|
---|
[02cea2d] | 346 | )
|
---|
| 347 | Cost newCost = conversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env );
|
---|
[a32b204] | 348 | PRINT(
|
---|
[6ed1d4b] | 349 | std::cerr << std::endl << "cost of conversion is " << newCost << std::endl;
|
---|
[02cea2d] | 350 | )
|
---|
| 351 | if ( newCost == Cost::infinity ) {
|
---|
| 352 | return newCost;
|
---|
| 353 | }
|
---|
[a32b204] | 354 | convCost += newCost;
|
---|
[d9a0e76] | 355 |
|
---|
[a32b204] | 356 | convCost += Cost( 0, polyCost( assert->second.formalType, alt.env, indexer ) + polyCost( assert->second.actualType, alt.env, indexer ), 0 );
|
---|
| 357 | }
|
---|
[d9a0e76] | 358 |
|
---|
[a32b204] | 359 | return convCost;
|
---|
| 360 | }
|
---|
[d9a0e76] | 361 |
|
---|
[8c84ebd] | 362 | /// Adds type variables to the open variable set and marks their assertions
|
---|
[a32b204] | 363 | void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ) {
|
---|
| 364 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
| 365 | unifiableVars[ (*tyvar)->get_name() ] = (*tyvar)->get_kind();
|
---|
| 366 | for ( std::list< DeclarationWithType* >::iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 367 | needAssertions[ *assert ] = true;
|
---|
| 368 | }
|
---|
[d9a0e76] | 369 | /// needAssertions.insert( needAssertions.end(), (*tyvar)->get_assertions().begin(), (*tyvar)->get_assertions().end() );
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
[a32b204] | 372 |
|
---|
| 373 | bool AlternativeFinder::instantiateFunction( std::list< DeclarationWithType* >& formals, /*const*/ AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave ) {
|
---|
| 374 | simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv );
|
---|
| 375 | // make sure we don't widen any existing bindings
|
---|
| 376 | for ( TypeEnvironment::iterator i = resultEnv.begin(); i != resultEnv.end(); ++i ) {
|
---|
| 377 | i->allowWidening = false;
|
---|
| 378 | }
|
---|
| 379 | resultEnv.extractOpenVars( openVars );
|
---|
| 380 |
|
---|
| 381 | std::list< DeclarationWithType* >::iterator formal = formals.begin();
|
---|
[0362d42] | 382 |
|
---|
| 383 | std::list< Type * > formalTypes;
|
---|
| 384 | std::list< Type * >::iterator formalType = formalTypes.end();
|
---|
| 385 |
|
---|
[a32b204] | 386 | for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) {
|
---|
[906e24d] | 387 | std::list< Type * > flatActualTypes;
|
---|
| 388 | flatten( actualExpr->expr->get_result(), back_inserter( flatActualTypes ) );
|
---|
[aa8f9df] | 389 | for ( std::list< Type* >::iterator actualType = flatActualTypes.begin(); actualType != flatActualTypes.end(); ++actualType, ++formalType ) {
|
---|
[0362d42] | 390 | if ( formalType == formalTypes.end() ) {
|
---|
| 391 | // the type of the formal parameter may be a tuple type. To make this easier to work with,
|
---|
| 392 | // flatten the tuple type and traverse the resulting list of types, incrementing the formal
|
---|
| 393 | // iterator once its types have been extracted. Once a particular formal parameter's type has
|
---|
| 394 | // been exhausted load the next formal parameter's type.
|
---|
| 395 | if ( formal == formals.end() ) {
|
---|
| 396 | return isVarArgs;
|
---|
| 397 | }
|
---|
| 398 | formalTypes.clear();
|
---|
| 399 | flatten( (*formal)->get_type(), back_inserter( formalTypes ) );
|
---|
| 400 | formalType = formalTypes.begin();
|
---|
| 401 | ++formal;
|
---|
[a32b204] | 402 | }
|
---|
| 403 | PRINT(
|
---|
| 404 | std::cerr << "formal type is ";
|
---|
[aa8f9df] | 405 | (*formalType)->print( std::cerr );
|
---|
[a32b204] | 406 | std::cerr << std::endl << "actual type is ";
|
---|
[0362d42] | 407 | (*actualType)->print( std::cerr );
|
---|
[a32b204] | 408 | std::cerr << std::endl;
|
---|
[7c64920] | 409 | )
|
---|
[0362d42] | 410 | if ( ! unify( *formalType, *actualType, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
|
---|
[0f19d763] | 411 | return false;
|
---|
| 412 | }
|
---|
[a32b204] | 413 | }
|
---|
| 414 | }
|
---|
[0362d42] | 415 |
|
---|
| 416 | // xxx - a tuple type was not completely matched
|
---|
| 417 | // partially handle the tuple with default arguments??
|
---|
| 418 | if ( formalType != formalTypes.end() ) return false;
|
---|
| 419 |
|
---|
[a32b204] | 420 | // Handling of default values
|
---|
| 421 | while ( formal != formals.end() ) {
|
---|
| 422 | if ( ObjectDecl *od = dynamic_cast<ObjectDecl *>( *formal ) )
|
---|
| 423 | if ( SingleInit *si = dynamic_cast<SingleInit *>( od->get_init() ))
|
---|
| 424 | // so far, only constant expressions are accepted as default values
|
---|
| 425 | if ( ConstantExpr *cnstexpr = dynamic_cast<ConstantExpr *>( si->get_value()) )
|
---|
| 426 | if ( Constant *cnst = dynamic_cast<Constant *>( cnstexpr->get_constant() ) )
|
---|
| 427 | if ( unify( (*formal)->get_type(), cnst->get_type(), resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
|
---|
| 428 | // XXX Don't know if this is right
|
---|
| 429 | actuals.push_back( Alternative( cnstexpr->clone(), env, Cost::zero ) );
|
---|
| 430 | formal++;
|
---|
| 431 | if ( formal == formals.end()) break;
|
---|
| 432 | }
|
---|
| 433 | return false;
|
---|
| 434 | }
|
---|
| 435 | return true;
|
---|
[d9a0e76] | 436 | }
|
---|
[51b73452] | 437 |
|
---|
[89b686a] | 438 | // /// Map of declaration uniqueIds (intended to be the assertions in an AssertionSet) to their parents and the number of times they've been included
|
---|
| 439 | //typedef std::unordered_map< UniqueId, std::unordered_map< UniqueId, unsigned > > AssertionParentSet;
|
---|
[79970ed] | 440 |
|
---|
[a1d7679] | 441 | static const int recursionLimit = /*10*/ 4; ///< Limit to depth of recursion satisfaction
|
---|
[89b686a] | 442 | //static const unsigned recursionParentLimit = 1; ///< Limit to the number of times an assertion can recursively use itself
|
---|
[51b73452] | 443 |
|
---|
[a32b204] | 444 | void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) {
|
---|
| 445 | for ( AssertionSet::iterator i = assertSet.begin(); i != assertSet.end(); ++i ) {
|
---|
| 446 | if ( i->second == true ) {
|
---|
| 447 | i->first->accept( indexer );
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
[d9a0e76] | 450 | }
|
---|
[79970ed] | 451 |
|
---|
[a32b204] | 452 | template< typename ForwardIterator, typename OutputIterator >
|
---|
[79970ed] | 453 | void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, /*const AssertionParentSet &needParents,*/
|
---|
[ebf5689] | 454 | int level, const SymTab::Indexer &indexer, OutputIterator out ) {
|
---|
[a32b204] | 455 | if ( begin == end ) {
|
---|
| 456 | if ( newNeed.empty() ) {
|
---|
| 457 | *out++ = newAlt;
|
---|
| 458 | return;
|
---|
| 459 | } else if ( level >= recursionLimit ) {
|
---|
| 460 | throw SemanticError( "Too many recursive assertions" );
|
---|
| 461 | } else {
|
---|
| 462 | AssertionSet newerNeed;
|
---|
| 463 | PRINT(
|
---|
| 464 | std::cerr << "recursing with new set:" << std::endl;
|
---|
| 465 | printAssertionSet( newNeed, std::cerr, 8 );
|
---|
[7c64920] | 466 | )
|
---|
[89b686a] | 467 | inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, /*needParents,*/ level+1, indexer, out );
|
---|
[a32b204] | 468 | return;
|
---|
| 469 | }
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | ForwardIterator cur = begin++;
|
---|
| 473 | if ( ! cur->second ) {
|
---|
[89b686a] | 474 | inferRecursive( begin, end, newAlt, openVars, decls, newNeed, /*needParents,*/ level, indexer, out );
|
---|
[a32b204] | 475 | }
|
---|
| 476 | DeclarationWithType *curDecl = cur->first;
|
---|
[d9a0e76] | 477 | PRINT(
|
---|
[a32b204] | 478 | std::cerr << "inferRecursive: assertion is ";
|
---|
| 479 | curDecl->print( std::cerr );
|
---|
| 480 | std::cerr << std::endl;
|
---|
[7c64920] | 481 | )
|
---|
[0f19d763] | 482 | std::list< DeclarationWithType* > candidates;
|
---|
[a32b204] | 483 | decls.lookupId( curDecl->get_name(), candidates );
|
---|
[6ed1d4b] | 484 | /// if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; }
|
---|
[a32b204] | 485 | for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) {
|
---|
| 486 | PRINT(
|
---|
[6ed1d4b] | 487 | std::cerr << "inferRecursive: candidate is ";
|
---|
| 488 | (*candidate)->print( std::cerr );
|
---|
| 489 | std::cerr << std::endl;
|
---|
[7c64920] | 490 | )
|
---|
[79970ed] | 491 |
|
---|
[0f19d763] | 492 | AssertionSet newHave, newerNeed( newNeed );
|
---|
[a32b204] | 493 | TypeEnvironment newEnv( newAlt.env );
|
---|
| 494 | OpenVarSet newOpenVars( openVars );
|
---|
| 495 | Type *adjType = (*candidate)->get_type()->clone();
|
---|
| 496 | adjustExprType( adjType, newEnv, indexer );
|
---|
| 497 | adjType->accept( global_renamer );
|
---|
| 498 | PRINT(
|
---|
| 499 | std::cerr << "unifying ";
|
---|
| 500 | curDecl->get_type()->print( std::cerr );
|
---|
| 501 | std::cerr << " with ";
|
---|
| 502 | adjType->print( std::cerr );
|
---|
| 503 | std::cerr << std::endl;
|
---|
[7c64920] | 504 | )
|
---|
[0f19d763] | 505 | if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) {
|
---|
| 506 | PRINT(
|
---|
| 507 | std::cerr << "success!" << std::endl;
|
---|
[a32b204] | 508 | )
|
---|
[0f19d763] | 509 | SymTab::Indexer newDecls( decls );
|
---|
| 510 | addToIndexer( newHave, newDecls );
|
---|
| 511 | Alternative newerAlt( newAlt );
|
---|
| 512 | newerAlt.env = newEnv;
|
---|
| 513 | assert( (*candidate)->get_uniqueId() );
|
---|
[22cad76] | 514 | DeclarationWithType *candDecl = static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) );
|
---|
[89b686a] | 515 | //AssertionParentSet newNeedParents( needParents );
|
---|
[22cad76] | 516 | // skip repeatingly-self-recursive assertion satisfaction
|
---|
[89b686a] | 517 | // DOESN'T WORK: grandchild nodes conflict with their cousins
|
---|
| 518 | //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue;
|
---|
[22cad76] | 519 | Expression *varExpr = new VariableExpr( candDecl );
|
---|
[906e24d] | 520 | delete varExpr->get_result();
|
---|
| 521 | varExpr->set_result( adjType->clone() );
|
---|
[0f19d763] | 522 | PRINT(
|
---|
[6ed1d4b] | 523 | std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " ";
|
---|
| 524 | curDecl->print( std::cerr );
|
---|
| 525 | std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " ";
|
---|
| 526 | (*candidate)->print( std::cerr );
|
---|
| 527 | std::cerr << std::endl;
|
---|
[7c64920] | 528 | )
|
---|
[0f19d763] | 529 | ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
|
---|
| 530 | // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
|
---|
| 531 | appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
|
---|
[89b686a] | 532 | inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out );
|
---|
[0f19d763] | 533 | } else {
|
---|
| 534 | delete adjType;
|
---|
| 535 | }
|
---|
[a32b204] | 536 | }
|
---|
[d9a0e76] | 537 | }
|
---|
| 538 |
|
---|
[a32b204] | 539 | template< typename OutputIterator >
|
---|
| 540 | void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {
|
---|
[d9a0e76] | 541 | // PRINT(
|
---|
[6ed1d4b] | 542 | // std::cerr << "inferParameters: assertions needed are" << std::endl;
|
---|
| 543 | // printAll( need, std::cerr, 8 );
|
---|
[d9a0e76] | 544 | // )
|
---|
[a32b204] | 545 | SymTab::Indexer decls( indexer );
|
---|
| 546 | PRINT(
|
---|
[6ed1d4b] | 547 | std::cerr << "============= original indexer" << std::endl;
|
---|
| 548 | indexer.print( std::cerr );
|
---|
| 549 | std::cerr << "============= new indexer" << std::endl;
|
---|
| 550 | decls.print( std::cerr );
|
---|
[7c64920] | 551 | )
|
---|
[0f19d763] | 552 | addToIndexer( have, decls );
|
---|
[a32b204] | 553 | AssertionSet newNeed;
|
---|
[89b686a] | 554 | //AssertionParentSet needParents;
|
---|
| 555 | inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, /*needParents,*/ 0, indexer, out );
|
---|
[d9a0e76] | 556 | // PRINT(
|
---|
[6ed1d4b] | 557 | // std::cerr << "declaration 14 is ";
|
---|
[d9a0e76] | 558 | // Declaration::declFromId
|
---|
| 559 | // *out++ = newAlt;
|
---|
| 560 | // )
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[a32b204] | 563 | template< typename OutputIterator >
|
---|
| 564 | void AlternativeFinder::makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, AltList &actualAlt, OutputIterator out ) {
|
---|
| 565 | OpenVarSet openVars;
|
---|
| 566 | AssertionSet resultNeed, resultHave;
|
---|
| 567 | TypeEnvironment resultEnv;
|
---|
| 568 | makeUnifiableVars( funcType, openVars, resultNeed );
|
---|
| 569 | if ( instantiateFunction( funcType->get_parameters(), actualAlt, funcType->get_isVarArgs(), openVars, resultEnv, resultNeed, resultHave ) ) {
|
---|
| 570 | ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
|
---|
| 571 | Alternative newAlt( appExpr, resultEnv, sumCost( actualAlt ) );
|
---|
| 572 | makeExprList( actualAlt, appExpr->get_args() );
|
---|
| 573 | PRINT(
|
---|
[6ed1d4b] | 574 | std::cerr << "need assertions:" << std::endl;
|
---|
| 575 | printAssertionSet( resultNeed, std::cerr, 8 );
|
---|
[7c64920] | 576 | )
|
---|
[0f19d763] | 577 | inferParameters( resultNeed, resultHave, newAlt, openVars, out );
|
---|
[d9a0e76] | 578 | }
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[a32b204] | 581 | void AlternativeFinder::visit( UntypedExpr *untypedExpr ) {
|
---|
| 582 | bool doneInit = false;
|
---|
| 583 | AlternativeFinder funcOpFinder( indexer, env );
|
---|
[d9a0e76] | 584 |
|
---|
[6ed1d4b] | 585 | AlternativeFinder funcFinder( indexer, env );
|
---|
| 586 |
|
---|
| 587 | {
|
---|
[70f89d00] | 588 | std::string fname = InitTweak::getFunctionName( untypedExpr );
|
---|
| 589 | if ( fname == "&&" ) {
|
---|
[2871210] | 590 | VoidType v = Type::Qualifiers(); // resolve to type void *
|
---|
| 591 | PointerType pt( Type::Qualifiers(), v.clone() );
|
---|
| 592 | UntypedExpr *vexpr = untypedExpr->clone();
|
---|
[906e24d] | 593 | vexpr->set_result( pt.clone() );
|
---|
[2871210] | 594 | alternatives.push_back( Alternative( vexpr, env, Cost()) );
|
---|
[a32b204] | 595 | return;
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
[d9a0e76] | 598 |
|
---|
[a32b204] | 599 | funcFinder.findWithAdjustment( untypedExpr->get_function() );
|
---|
| 600 | std::list< AlternativeFinder > argAlternatives;
|
---|
| 601 | findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(), back_inserter( argAlternatives ) );
|
---|
[d9a0e76] | 602 |
|
---|
[a32b204] | 603 | std::list< AltList > possibilities;
|
---|
| 604 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) );
|
---|
[d9a0e76] | 605 |
|
---|
[5af62f1] | 606 | // take care of possible tuple assignments
|
---|
| 607 | // if not tuple assignment, assignment is taken care of as a normal function call
|
---|
| 608 | Tuples::handleTupleAssignment( *this, untypedExpr, possibilities );
|
---|
[d9a0e76] | 609 |
|
---|
[a32b204] | 610 | AltList candidates;
|
---|
[91b8a17] | 611 | SemanticError errors;
|
---|
[d9a0e76] | 612 |
|
---|
[a32b204] | 613 | for ( AltList::const_iterator func = funcFinder.alternatives.begin(); func != funcFinder.alternatives.end(); ++func ) {
|
---|
[91b8a17] | 614 | try {
|
---|
| 615 | PRINT(
|
---|
| 616 | std::cerr << "working on alternative: " << std::endl;
|
---|
| 617 | func->print( std::cerr, 8 );
|
---|
| 618 | )
|
---|
| 619 | // check if the type is pointer to function
|
---|
| 620 | PointerType *pointer;
|
---|
[906e24d] | 621 | if ( ( pointer = dynamic_cast< PointerType* >( func->expr->get_result() ) ) ) {
|
---|
[91b8a17] | 622 | if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
|
---|
| 623 | for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
|
---|
| 624 | // XXX
|
---|
| 625 | //Designators::check_alternative( function, *actualAlt );
|
---|
| 626 | makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) );
|
---|
| 627 | }
|
---|
| 628 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( pointer->get_base() ) ) {
|
---|
| 629 | EqvClass eqvClass;
|
---|
| 630 | if ( func->env.lookup( typeInst->get_name(), eqvClass ) && eqvClass.type ) {
|
---|
| 631 | if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.type ) ) {
|
---|
| 632 | for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
|
---|
| 633 | makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) );
|
---|
| 634 | } // for
|
---|
| 635 | } // if
|
---|
[a32b204] | 636 | } // if
|
---|
| 637 | } // if
|
---|
[91b8a17] | 638 | } else {
|
---|
| 639 | // seek a function operator that's compatible
|
---|
| 640 | if ( ! doneInit ) {
|
---|
| 641 | doneInit = true;
|
---|
| 642 | NameExpr *opExpr = new NameExpr( "?()" );
|
---|
| 643 | try {
|
---|
| 644 | funcOpFinder.findWithAdjustment( opExpr );
|
---|
| 645 | } catch( SemanticError &e ) {
|
---|
| 646 | // it's ok if there aren't any defined function ops
|
---|
| 647 | }
|
---|
| 648 | PRINT(
|
---|
| 649 | std::cerr << "known function ops:" << std::endl;
|
---|
| 650 | printAlts( funcOpFinder.alternatives, std::cerr, 8 );
|
---|
| 651 | )
|
---|
[a32b204] | 652 | }
|
---|
| 653 |
|
---|
[91b8a17] | 654 | for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) {
|
---|
| 655 | // check if the type is pointer to function
|
---|
| 656 | PointerType *pointer;
|
---|
[906e24d] | 657 | if ( ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_result() ) ) ) {
|
---|
[91b8a17] | 658 | if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
|
---|
| 659 | for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) {
|
---|
| 660 | AltList currentAlt;
|
---|
| 661 | currentAlt.push_back( *func );
|
---|
| 662 | currentAlt.insert( currentAlt.end(), actualAlt->begin(), actualAlt->end() );
|
---|
| 663 | makeFunctionAlternatives( *funcOp, function, currentAlt, std::back_inserter( candidates ) );
|
---|
| 664 | } // for
|
---|
| 665 | } // if
|
---|
[a32b204] | 666 | } // if
|
---|
[91b8a17] | 667 | } // for
|
---|
| 668 | } // if
|
---|
| 669 | } catch ( SemanticError &e ) {
|
---|
| 670 | errors.append( e );
|
---|
| 671 | }
|
---|
[a32b204] | 672 | } // for
|
---|
| 673 |
|
---|
[91b8a17] | 674 | // Implement SFINAE; resolution errors are only errors if there aren't any non-erroneous resolutions
|
---|
| 675 | if ( candidates.empty() && ! errors.isEmpty() ) { throw errors; }
|
---|
| 676 |
|
---|
[a32b204] | 677 | for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) {
|
---|
| 678 | Cost cvtCost = computeConversionCost( *withFunc, indexer );
|
---|
| 679 |
|
---|
| 680 | PRINT(
|
---|
[8f7cea1] | 681 | ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( withFunc->expr );
|
---|
[906e24d] | 682 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
|
---|
[8f7cea1] | 683 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
|
---|
[6ed1d4b] | 684 | std::cerr << "Case +++++++++++++" << std::endl;
|
---|
| 685 | std::cerr << "formals are:" << std::endl;
|
---|
| 686 | printAll( function->get_parameters(), std::cerr, 8 );
|
---|
| 687 | std::cerr << "actuals are:" << std::endl;
|
---|
| 688 | printAll( appExpr->get_args(), std::cerr, 8 );
|
---|
| 689 | std::cerr << "bindings are:" << std::endl;
|
---|
| 690 | withFunc->env.print( std::cerr, 8 );
|
---|
| 691 | std::cerr << "cost of conversion is:" << cvtCost << std::endl;
|
---|
[7c64920] | 692 | )
|
---|
| 693 | if ( cvtCost != Cost::infinity ) {
|
---|
| 694 | withFunc->cvtCost = cvtCost;
|
---|
| 695 | alternatives.push_back( *withFunc );
|
---|
| 696 | } // if
|
---|
[a32b204] | 697 | } // for
|
---|
| 698 | candidates.clear();
|
---|
| 699 | candidates.splice( candidates.end(), alternatives );
|
---|
| 700 |
|
---|
| 701 | findMinCost( candidates.begin(), candidates.end(), std::back_inserter( alternatives ) );
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | bool isLvalue( Expression *expr ) {
|
---|
[906e24d] | 705 | // xxx - recurse into tuples?
|
---|
| 706 | return expr->has_result() && expr->get_result()->get_isLvalue();
|
---|
[a32b204] | 707 | }
|
---|
| 708 |
|
---|
| 709 | void AlternativeFinder::visit( AddressExpr *addressExpr ) {
|
---|
| 710 | AlternativeFinder finder( indexer, env );
|
---|
| 711 | finder.find( addressExpr->get_arg() );
|
---|
| 712 | for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
|
---|
| 713 | if ( isLvalue( i->expr ) ) {
|
---|
| 714 | alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) );
|
---|
| 715 | } // if
|
---|
| 716 | } // for
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | void AlternativeFinder::visit( CastExpr *castExpr ) {
|
---|
[906e24d] | 720 | Type *& toType = castExpr->get_result();
|
---|
| 721 | toType = resolveTypeof( toType, indexer );
|
---|
| 722 | SymTab::validateType( toType, &indexer );
|
---|
| 723 | adjustExprType( toType, env, indexer );
|
---|
[a32b204] | 724 |
|
---|
| 725 | AlternativeFinder finder( indexer, env );
|
---|
| 726 | finder.findWithAdjustment( castExpr->get_arg() );
|
---|
| 727 |
|
---|
| 728 | AltList candidates;
|
---|
| 729 | for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
|
---|
| 730 | AssertionSet needAssertions, haveAssertions;
|
---|
| 731 | OpenVarSet openVars;
|
---|
| 732 |
|
---|
| 733 | // It's possible that a cast can throw away some values in a multiply-valued expression. (An example is a
|
---|
| 734 | // cast-to-void, which casts from one value to zero.) Figure out the prefix of the subexpression results
|
---|
| 735 | // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast
|
---|
| 736 | // to.
|
---|
[906e24d] | 737 | int discardedValues = (*i).expr->get_result()->size() - castExpr->get_result()->size();
|
---|
[a32b204] | 738 | if ( discardedValues < 0 ) continue;
|
---|
[906e24d] | 739 | // xxx - may need to go into tuple types and extract relavent types and use unifyList
|
---|
[adcdd2f] | 740 | // unification run for side-effects
|
---|
[906e24d] | 741 | unify( castExpr->get_result(), (*i).expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer );
|
---|
| 742 | Cost thisCost = castCost( (*i).expr->get_result(), castExpr->get_result(), indexer, i->env );
|
---|
[a32b204] | 743 | if ( thisCost != Cost::infinity ) {
|
---|
| 744 | // count one safe conversion for each value that is thrown away
|
---|
| 745 | thisCost += Cost( 0, 0, discardedValues );
|
---|
| 746 | CastExpr *newExpr = castExpr->clone();
|
---|
| 747 | newExpr->set_arg( i->expr->clone() );
|
---|
| 748 | candidates.push_back( Alternative( newExpr, i->env, i->cost, thisCost ) );
|
---|
| 749 | } // if
|
---|
| 750 | } // for
|
---|
| 751 |
|
---|
| 752 | // findMinCost selects the alternatives with the lowest "cost" members, but has the side effect of copying the
|
---|
| 753 | // cvtCost member to the cost member (since the old cost is now irrelevant). Thus, calling findMinCost twice
|
---|
| 754 | // selects first based on argument cost, then on conversion cost.
|
---|
| 755 | AltList minArgCost;
|
---|
| 756 | findMinCost( candidates.begin(), candidates.end(), std::back_inserter( minArgCost ) );
|
---|
| 757 | findMinCost( minArgCost.begin(), minArgCost.end(), std::back_inserter( alternatives ) );
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) {
|
---|
| 761 | AlternativeFinder funcFinder( indexer, env );
|
---|
| 762 | funcFinder.findWithAdjustment( memberExpr->get_aggregate() );
|
---|
| 763 |
|
---|
| 764 | for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) {
|
---|
[906e24d] | 765 | if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_result() ) ) {
|
---|
| 766 | addAggMembers( structInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() );
|
---|
| 767 | } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_result() ) ) {
|
---|
| 768 | addAggMembers( unionInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() );
|
---|
[a32b204] | 769 | } // if
|
---|
| 770 | } // for
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | void AlternativeFinder::visit( MemberExpr *memberExpr ) {
|
---|
| 774 | alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) );
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | void AlternativeFinder::visit( NameExpr *nameExpr ) {
|
---|
| 778 | std::list< DeclarationWithType* > declList;
|
---|
| 779 | indexer.lookupId( nameExpr->get_name(), declList );
|
---|
| 780 | PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; )
|
---|
[0f19d763] | 781 | for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) {
|
---|
| 782 | VariableExpr newExpr( *i, nameExpr->get_argName() );
|
---|
| 783 | alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
|
---|
| 784 | PRINT(
|
---|
| 785 | std::cerr << "decl is ";
|
---|
| 786 | (*i)->print( std::cerr );
|
---|
| 787 | std::cerr << std::endl;
|
---|
| 788 | std::cerr << "newExpr is ";
|
---|
| 789 | newExpr.print( std::cerr );
|
---|
| 790 | std::cerr << std::endl;
|
---|
[7c64920] | 791 | )
|
---|
[0f19d763] | 792 | renameTypes( alternatives.back().expr );
|
---|
| 793 | if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
|
---|
[3b58d91] | 794 | NameExpr nameExpr( "" );
|
---|
[add7117] | 795 | addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), env, &nameExpr );
|
---|
[0f19d763] | 796 | } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) {
|
---|
[3b58d91] | 797 | NameExpr nameExpr( "" );
|
---|
[add7117] | 798 | addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), env, &nameExpr );
|
---|
[0f19d763] | 799 | } // if
|
---|
| 800 | } // for
|
---|
[a32b204] | 801 | }
|
---|
| 802 |
|
---|
| 803 | void AlternativeFinder::visit( VariableExpr *variableExpr ) {
|
---|
[85517ddb] | 804 | // not sufficient to clone here, because variable's type may have changed
|
---|
| 805 | // since the VariableExpr was originally created.
|
---|
| 806 | alternatives.push_back( Alternative( new VariableExpr( variableExpr->get_var() ), env, Cost::zero ) );
|
---|
[a32b204] | 807 | }
|
---|
| 808 |
|
---|
| 809 | void AlternativeFinder::visit( ConstantExpr *constantExpr ) {
|
---|
| 810 | alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) );
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) {
|
---|
| 814 | if ( sizeofExpr->get_isType() ) {
|
---|
[85517ddb] | 815 | // xxx - resolveTypeof?
|
---|
[a32b204] | 816 | alternatives.push_back( Alternative( sizeofExpr->clone(), env, Cost::zero ) );
|
---|
| 817 | } else {
|
---|
| 818 | // find all alternatives for the argument to sizeof
|
---|
| 819 | AlternativeFinder finder( indexer, env );
|
---|
| 820 | finder.find( sizeofExpr->get_expr() );
|
---|
| 821 | // find the lowest cost alternative among the alternatives, otherwise ambiguous
|
---|
| 822 | AltList winners;
|
---|
| 823 | findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) );
|
---|
| 824 | if ( winners.size() != 1 ) {
|
---|
| 825 | throw SemanticError( "Ambiguous expression in sizeof operand: ", sizeofExpr->get_expr() );
|
---|
| 826 | } // if
|
---|
| 827 | // return the lowest cost alternative for the argument
|
---|
| 828 | Alternative &choice = winners.front();
|
---|
| 829 | alternatives.push_back( Alternative( new SizeofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
|
---|
[47534159] | 830 | } // if
|
---|
| 831 | }
|
---|
| 832 |
|
---|
| 833 | void AlternativeFinder::visit( AlignofExpr *alignofExpr ) {
|
---|
| 834 | if ( alignofExpr->get_isType() ) {
|
---|
[85517ddb] | 835 | // xxx - resolveTypeof?
|
---|
[47534159] | 836 | alternatives.push_back( Alternative( alignofExpr->clone(), env, Cost::zero ) );
|
---|
| 837 | } else {
|
---|
| 838 | // find all alternatives for the argument to sizeof
|
---|
| 839 | AlternativeFinder finder( indexer, env );
|
---|
| 840 | finder.find( alignofExpr->get_expr() );
|
---|
| 841 | // find the lowest cost alternative among the alternatives, otherwise ambiguous
|
---|
| 842 | AltList winners;
|
---|
| 843 | findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) );
|
---|
| 844 | if ( winners.size() != 1 ) {
|
---|
| 845 | throw SemanticError( "Ambiguous expression in alignof operand: ", alignofExpr->get_expr() );
|
---|
| 846 | } // if
|
---|
| 847 | // return the lowest cost alternative for the argument
|
---|
| 848 | Alternative &choice = winners.front();
|
---|
| 849 | alternatives.push_back( Alternative( new AlignofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
|
---|
[a32b204] | 850 | } // if
|
---|
| 851 | }
|
---|
| 852 |
|
---|
[2a4b088] | 853 | template< typename StructOrUnionType >
|
---|
| 854 | void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) {
|
---|
| 855 | std::list< Declaration* > members;
|
---|
| 856 | aggInst->lookup( name, members );
|
---|
| 857 | for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
|
---|
| 858 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
|
---|
[79970ed] | 859 | alternatives.push_back( Alternative( new OffsetofExpr( aggInst->clone(), dwt ), env, Cost::zero ) );
|
---|
[2a4b088] | 860 | renameTypes( alternatives.back().expr );
|
---|
| 861 | } else {
|
---|
| 862 | assert( false );
|
---|
| 863 | }
|
---|
| 864 | }
|
---|
| 865 | }
|
---|
[6ed1d4b] | 866 |
|
---|
[2a4b088] | 867 | void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) {
|
---|
| 868 | AlternativeFinder funcFinder( indexer, env );
|
---|
[85517ddb] | 869 | // xxx - resolveTypeof?
|
---|
[2a4b088] | 870 | if ( StructInstType *structInst = dynamic_cast< StructInstType* >( offsetofExpr->get_type() ) ) {
|
---|
| 871 | addOffsetof( structInst, offsetofExpr->get_member() );
|
---|
| 872 | } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( offsetofExpr->get_type() ) ) {
|
---|
| 873 | addOffsetof( unionInst, offsetofExpr->get_member() );
|
---|
| 874 | }
|
---|
| 875 | }
|
---|
[6ed1d4b] | 876 |
|
---|
[25a054f] | 877 | void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) {
|
---|
| 878 | alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) );
|
---|
[afc1045] | 879 | }
|
---|
| 880 |
|
---|
| 881 | void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) {
|
---|
| 882 | alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) );
|
---|
[25a054f] | 883 | }
|
---|
| 884 |
|
---|
[a32b204] | 885 | void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) {
|
---|
| 886 | // assume no polymorphism
|
---|
| 887 | // assume no implicit conversions
|
---|
| 888 | assert( function->get_parameters().size() == 1 );
|
---|
| 889 | PRINT(
|
---|
[6ed1d4b] | 890 | std::cerr << "resolvAttr: funcDecl is ";
|
---|
| 891 | funcDecl->print( std::cerr );
|
---|
| 892 | std::cerr << " argType is ";
|
---|
| 893 | argType->print( std::cerr );
|
---|
| 894 | std::cerr << std::endl;
|
---|
[7c64920] | 895 | )
|
---|
| 896 | if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
|
---|
| 897 | alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
|
---|
| 898 | for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
|
---|
[906e24d] | 899 | alternatives.back().expr->set_result( (*i)->get_type()->clone() );
|
---|
[7c64920] | 900 | } // for
|
---|
| 901 | } // if
|
---|
[a32b204] | 902 | }
|
---|
| 903 |
|
---|
| 904 | void AlternativeFinder::visit( AttrExpr *attrExpr ) {
|
---|
| 905 | // assume no 'pointer-to-attribute'
|
---|
| 906 | NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
|
---|
| 907 | assert( nameExpr );
|
---|
| 908 | std::list< DeclarationWithType* > attrList;
|
---|
| 909 | indexer.lookupId( nameExpr->get_name(), attrList );
|
---|
| 910 | if ( attrExpr->get_isType() || attrExpr->get_expr() ) {
|
---|
| 911 | for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
|
---|
| 912 | // check if the type is function
|
---|
| 913 | if ( FunctionType *function = dynamic_cast< FunctionType* >( (*i)->get_type() ) ) {
|
---|
| 914 | // assume exactly one parameter
|
---|
| 915 | if ( function->get_parameters().size() == 1 ) {
|
---|
| 916 | if ( attrExpr->get_isType() ) {
|
---|
| 917 | resolveAttr( *i, function, attrExpr->get_type(), env );
|
---|
| 918 | } else {
|
---|
| 919 | AlternativeFinder finder( indexer, env );
|
---|
| 920 | finder.find( attrExpr->get_expr() );
|
---|
| 921 | for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) {
|
---|
[906e24d] | 922 | if ( choice->expr->get_result()->size() == 1 ) {
|
---|
| 923 | resolveAttr(*i, function, choice->expr->get_result(), choice->env );
|
---|
[a32b204] | 924 | } // fi
|
---|
| 925 | } // for
|
---|
| 926 | } // if
|
---|
| 927 | } // if
|
---|
| 928 | } // if
|
---|
| 929 | } // for
|
---|
| 930 | } else {
|
---|
| 931 | for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
|
---|
| 932 | VariableExpr newExpr( *i );
|
---|
| 933 | alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
|
---|
| 934 | renameTypes( alternatives.back().expr );
|
---|
| 935 | } // for
|
---|
| 936 | } // if
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | void AlternativeFinder::visit( LogicalExpr *logicalExpr ) {
|
---|
| 940 | AlternativeFinder firstFinder( indexer, env );
|
---|
| 941 | firstFinder.findWithAdjustment( logicalExpr->get_arg1() );
|
---|
| 942 | for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) {
|
---|
| 943 | AlternativeFinder secondFinder( indexer, first->env );
|
---|
| 944 | secondFinder.findWithAdjustment( logicalExpr->get_arg2() );
|
---|
| 945 | for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) {
|
---|
| 946 | LogicalExpr *newExpr = new LogicalExpr( first->expr->clone(), second->expr->clone(), logicalExpr->get_isAnd() );
|
---|
| 947 | alternatives.push_back( Alternative( newExpr, second->env, first->cost + second->cost ) );
|
---|
[d9a0e76] | 948 | }
|
---|
| 949 | }
|
---|
| 950 | }
|
---|
[51b73452] | 951 |
|
---|
[a32b204] | 952 | void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) {
|
---|
| 953 | AlternativeFinder firstFinder( indexer, env );
|
---|
| 954 | firstFinder.findWithAdjustment( conditionalExpr->get_arg1() );
|
---|
| 955 | for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) {
|
---|
| 956 | AlternativeFinder secondFinder( indexer, first->env );
|
---|
| 957 | secondFinder.findWithAdjustment( conditionalExpr->get_arg2() );
|
---|
| 958 | for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) {
|
---|
| 959 | AlternativeFinder thirdFinder( indexer, second->env );
|
---|
| 960 | thirdFinder.findWithAdjustment( conditionalExpr->get_arg3() );
|
---|
| 961 | for ( AltList::const_iterator third = thirdFinder.alternatives.begin(); third != thirdFinder.alternatives.end(); ++third ) {
|
---|
| 962 | OpenVarSet openVars;
|
---|
| 963 | AssertionSet needAssertions, haveAssertions;
|
---|
| 964 | Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost );
|
---|
[906e24d] | 965 | Type* commonType;
|
---|
| 966 | if ( unify( second->expr->get_result(), third->expr->get_result(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) {
|
---|
[a32b204] | 967 | ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() );
|
---|
[906e24d] | 968 | newExpr->set_result( commonType ? commonType : second->expr->get_result()->clone() );
|
---|
[a32b204] | 969 | newAlt.expr = newExpr;
|
---|
| 970 | inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) );
|
---|
| 971 | } // if
|
---|
| 972 | } // for
|
---|
| 973 | } // for
|
---|
| 974 | } // for
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | void AlternativeFinder::visit( CommaExpr *commaExpr ) {
|
---|
| 978 | TypeEnvironment newEnv( env );
|
---|
| 979 | Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv );
|
---|
| 980 | AlternativeFinder secondFinder( indexer, newEnv );
|
---|
| 981 | secondFinder.findWithAdjustment( commaExpr->get_arg2() );
|
---|
| 982 | for ( AltList::const_iterator alt = secondFinder.alternatives.begin(); alt != secondFinder.alternatives.end(); ++alt ) {
|
---|
| 983 | alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt->expr->clone() ), alt->env, alt->cost ) );
|
---|
| 984 | } // for
|
---|
| 985 | delete newFirstArg;
|
---|
| 986 | }
|
---|
| 987 |
|
---|
| 988 | void AlternativeFinder::visit( TupleExpr *tupleExpr ) {
|
---|
| 989 | std::list< AlternativeFinder > subExprAlternatives;
|
---|
| 990 | findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) );
|
---|
| 991 | std::list< AltList > possibilities;
|
---|
| 992 | combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) );
|
---|
| 993 | for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) {
|
---|
| 994 | TupleExpr *newExpr = new TupleExpr;
|
---|
| 995 | makeExprList( *i, newExpr->get_exprs() );
|
---|
[aa8f9df] | 996 | TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, true) );
|
---|
| 997 | Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
|
---|
[906e24d] | 998 | for ( Expression * resultExpr : newExpr->get_exprs() ) {
|
---|
[aa8f9df] | 999 | Type * type = resultExpr->get_result()->clone();
|
---|
| 1000 | tupleType->get_types().push_back( type );
|
---|
| 1001 | qualifiers &= type->get_qualifiers();
|
---|
[a32b204] | 1002 | } // for
|
---|
[aa8f9df] | 1003 | newExpr->set_result( tupleType );
|
---|
[a32b204] | 1004 |
|
---|
| 1005 | TypeEnvironment compositeEnv;
|
---|
| 1006 | simpleCombineEnvironments( i->begin(), i->end(), compositeEnv );
|
---|
| 1007 | alternatives.push_back( Alternative( newExpr, compositeEnv, sumCost( *i ) ) );
|
---|
| 1008 | } // for
|
---|
[d9a0e76] | 1009 | }
|
---|
[dc2e7e0] | 1010 |
|
---|
| 1011 | void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) {
|
---|
| 1012 | alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) );
|
---|
| 1013 | }
|
---|
[b6fe7e6] | 1014 |
|
---|
| 1015 | void AlternativeFinder::visit( ConstructorExpr * ctorExpr ) {
|
---|
| 1016 | AlternativeFinder finder( indexer, env );
|
---|
| 1017 | // don't prune here, since it's guaranteed all alternatives will have the same type
|
---|
| 1018 | // (giving the alternatives different types is half of the point of ConstructorExpr nodes)
|
---|
| 1019 | finder.findWithAdjustment( ctorExpr->get_callExpr(), false );
|
---|
| 1020 | for ( Alternative & alt : finder.alternatives ) {
|
---|
| 1021 | alternatives.push_back( Alternative( new ConstructorExpr( alt.expr->clone() ), alt.env, alt.cost ) );
|
---|
| 1022 | }
|
---|
| 1023 | }
|
---|
[8f7cea1] | 1024 |
|
---|
| 1025 | void AlternativeFinder::visit( TupleIndexExpr *tupleExpr ) {
|
---|
| 1026 | alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) );
|
---|
| 1027 | }
|
---|
[aa8f9df] | 1028 |
|
---|
| 1029 | void AlternativeFinder::visit( TupleAssignExpr *tupleAssignExpr ) {
|
---|
| 1030 | alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) );
|
---|
| 1031 | }
|
---|
[51b73452] | 1032 | } // namespace ResolvExpr
|
---|
[a32b204] | 1033 |
|
---|
| 1034 | // Local Variables: //
|
---|
| 1035 | // tab-width: 4 //
|
---|
| 1036 | // mode: c++ //
|
---|
| 1037 | // compile-command: "make install" //
|
---|
| 1038 | // End: //
|
---|